home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / htdocs / phpmyadmin / tbl_select.php < prev    next >
PHP Script  |  2002-01-06  |  8KB  |  211 lines

  1. <?php
  2. /* $Id: tbl_select.php,v 1.29 2001/12/24 11:18:29 loic1 Exp $ */
  3.  
  4.  
  5. /**
  6.  * Gets some core libraries
  7.  */
  8. require('./libraries/grab_globals.lib.php');
  9. require('./libraries/common.lib.php');
  10.  
  11.  
  12. /**
  13.  * Defines the url to return to in case of error in a sql statement
  14.  */
  15. $err_url = $goto
  16.          . '?lang=' . $lang
  17.          . '&server=' . $server
  18.          . '&db=' . urlencode($db)
  19.          . '&table=' . urlencode($table);
  20.  
  21.  
  22. /**
  23.  * Not selection yet required -> displays the selection form
  24.  */
  25. if (!isset($param) || $param[0] == '') {
  26.     include('./header.inc.php');
  27.  
  28.     // Gets the list and number of fields
  29.     $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
  30.     $result      = @mysql_query($local_query);
  31.     if (!$result) {
  32.         PMA_mysqlDie('', $local_query, '', $err_url);
  33.     }
  34.     else {
  35.         $fields_cnt        = mysql_num_rows($result);
  36.         while ($row = mysql_fetch_array($result)) {
  37.             $fields_list[] = $row['Field'];
  38.             $type          = $row['Type'];
  39.             // reformat mysql query output - staybyte - 9. June 2001
  40.             $shorttype     = substr($type, 0, 3);
  41.             if ($shorttype == 'set' || $shorttype == 'enu') {
  42.                 $type      = eregi_replace(',', ', ', $type);
  43.                 // Removes automatic MySQL escape format
  44.                 $type      = str_replace('\'\'', '\\\'', $type);
  45.             }
  46.             $type          = eregi_replace('BINARY', '', $type);
  47.             $type          = eregi_replace('ZEROFILL', '', $type);
  48.             $type          = eregi_replace('UNSIGNED', '', $type);
  49.             if (empty($type)) {
  50.                 $type      = ' ';
  51.             }
  52.             $fields_type[] = $type;
  53.         } // end while
  54.         mysql_free_result($result);
  55.         ?>
  56. <form method="post" action="tbl_select.php">
  57.     <input type="hidden" name="server" value="<?php echo $server; ?>" />
  58.     <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
  59.     <input type="hidden" name="db" value="<?php echo $db; ?>" />
  60.     <input type="hidden" name="table" value="<?php echo $table; ?>" />
  61.     <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
  62.          
  63.     <?php echo $strSelectFields; ?> :<br />
  64.          
  65.     <select name="param[]" size="10" multiple="multiple">
  66.         <?php
  67.         echo "\n";
  68.         // Displays the list of the fields
  69.         for ($i = 0 ; $i < $fields_cnt; $i++) {
  70.             echo '        <option value="' . urlencode($fields_list[$i]) . '" selected="selected">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
  71.         }
  72.         ?>
  73.     </select><br />
  74.     <ul>
  75.         <li>
  76.             <div style="margin-bottom: 10px">
  77.             <?php echo $strLimitNumRows . "\n"; ?>
  78.             <input type="text" size="4" name="session_max_rows" value="<?php echo $cfgMaxRows; ?>" />
  79.             </div>
  80.         </li>
  81.         <li>
  82.             <?php echo $strAddSearchConditions; ?><br />
  83.             <input type="text" name="where" /> 
  84.             <?php echo PMA_showDocuShort('F/u/Functions.html') . "\n"; ?>
  85.             <br /><br />
  86.             <?php echo '<i>' . $strOr . '</i> ' . $strDoAQuery; ?><br />
  87.             <table border="<?php echo $cfgBorder; ?>">
  88.             <tr>
  89.                 <th><?php echo $strField; ?></th>
  90.                 <th><?php echo $strType; ?></th>
  91.                 <th><?php echo $strValue; ?></th>
  92.             </tr>
  93.         <?php
  94.         for ($i = 0; $i < $fields_cnt; $i++) {
  95.             echo "\n";
  96.             $bgcolor   = ($i % 2) ? $cfgBgcolorOne : $cfgBgcolorTwo;
  97.             ?>
  98.             <tr>
  99.                 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo htmlspecialchars($fields_list[$i]); ?></td>
  100.                 <td bgcolor="<?php echo $bgcolor; ?>"><?php echo $fields_type[$i]; ?></td>
  101.                 <td bgcolor="<?php echo $bgcolor; ?>">
  102.                     <input type="text" name="fields[]" size="40" />
  103.                     <input type="hidden" name="names[]" value="<?php echo urlencode($fields_list[$i]); ?>" />
  104.                     <input type="hidden" name="types[]" value="<?php echo $fields_type[$i]; ?>" />
  105.                 </td>
  106.             </tr>
  107.             <?php
  108.         } // end for
  109.         echo "\n";
  110.         ?>
  111.             </table><br />
  112.         </li>
  113.         <li>
  114.             <?php echo $strDisplayOrder; ?><br />
  115.             <select name="orderField" style="vertical-align: middle">
  116.                 <option value="--nil--"></option>
  117.         <?php
  118.         echo "\n";
  119.         for ($i = 0; $i < $fields_cnt; $i++) {
  120.             echo '                ';
  121.             echo '<option value="' . urlencode($fields_list[$i]) . '">' . htmlspecialchars($fields_list[$i]) . '</option>' . "\n";
  122.         } // end for
  123.         ?>
  124.             </select>
  125.             <input type="radio" name="order" value="ASC" checked="checked" />
  126.             <?php echo $strAscending; ?> 
  127.             <input type="radio" name="order" value="DESC" />
  128.             <?php echo $strDescending; ?><br /><br />
  129.         </li>
  130.     </ul>
  131.  
  132.         
  133.     <input type="submit" name="submit" value="<?php echo $strGo; ?>" />
  134. </form>
  135.         <?php
  136.     } // end if
  137.     echo "\n";
  138.     include('./footer.inc.php');
  139. }
  140.  
  141.  
  142. /**
  143.  * Selection criteria have been submitted -> do the work
  144.  */
  145. else {
  146.     // Builds the query
  147.     $sql_query = 'SELECT ' . PMA_backquote(urldecode($param[0]));
  148.     $i         = 0;
  149.     $c         = count($param);
  150.     while ($i < $c) {
  151.         if ($i > 0) {
  152.             $sql_query .= ',' . PMA_backquote(urldecode($param[$i]));
  153.         }
  154.         $i++;
  155.     }
  156.     $sql_query .= ' FROM ' . PMA_backquote($table);
  157.     // The where clause
  158.     if ($where != '') {
  159.         $sql_query .= ' WHERE ' . ((get_magic_quotes_gpc()) ? stripslashes($where) : $where);
  160.     }
  161.     else {
  162.         $sql_query .= ' WHERE 1';
  163.         for ($i = 0; $i < count($fields); $i++) {
  164.             if (!empty($fields) && $fields[$i] != '') {
  165.                 if (strtoupper($fields[$i]) == 'NULL' || strtoupper($fields[$i]) == 'NOT NULL') {
  166.                     $quot = '';
  167.                     $cmp  = 'IS';
  168.                 }
  169.                 else if (eregi('char|blob|text', $types[$i])
  170.                          || eregi('(set|enum)[(]', $types[$i])) {
  171.                     $quot = '\'';
  172.                     $cmp  = 'LIKE';
  173.                     if (get_magic_quotes_gpc()) {
  174.                         $fields[$i] = stripslashes($fields[$i]);
  175.                     }
  176.                     $fields[$i]     = PMA_sqlAddslashes($fields[$i], TRUE);
  177.                 }
  178.                 else if (eregi('date|time|year', $types[$i])) {
  179.                     $quot = '\'';
  180.                     $cmp  = '=';
  181.                 }
  182.                 else if (strstr($fields[$i], '%')) {
  183.                     $quot = '\'';
  184.                     $cmp  = 'LIKE';
  185.                 }
  186.                 else if (substr($fields[$i], 0, 1) == '<' || substr($fields[$i], 0, 1) == '>') {
  187.                     $quot = '';
  188.                     $cmp  = '';
  189.                 }
  190.                 else {
  191.                     $quot = '';
  192.                     $cmp  = '=';
  193.                 } // end if
  194.                 $sql_query .= ' AND ' . PMA_backquote(urldecode($names[$i])) . " $cmp $quot$fields[$i]$quot";
  195.             } // end if
  196.         } // end for
  197.     } // end if
  198.     if ($orderField != '--nil--') {
  199.         $sql_query .= ' ORDER BY ' . PMA_backquote(urldecode($orderField)) . ' ' . $order;
  200.     } // end if
  201.  
  202.     // The query will be stripslashed in sql.php if "magic_quotes_gpc" is on
  203.     if (get_magic_quotes_gpc()) {
  204.         $sql_query = addslashes($sql_query);
  205.     }
  206.     include('./sql.php');
  207.  
  208. }
  209.  
  210. ?>
  211.