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_replace.php < prev    next >
PHP Script  |  2002-01-06  |  9KB  |  257 lines

  1. <?php
  2. /* $Id: tbl_replace.php,v 1.44 2002/01/02 00:09:42 lem9 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.  * Initializes some variables
  14.  */
  15. // Defines the url to return in case of success of the query
  16. if (isset($sql_query)) {
  17.     $sql_query = urldecode($sql_query);
  18. }
  19. if (isset($after_insert) && $after_insert == 'new_insert') {
  20.     $goto = 'tbl_change.php'
  21.           . '?lang=' . $lang
  22.           . '&server=' . $server
  23.           . '&db=' . urlencode($db)
  24.           . '&table=' . urlencode($table)
  25.           . '&goto=' . urlencode($goto)
  26.           . (empty($primary_key) ? '' : '&primary_key=' . $primary_key)
  27.           . '&pos=' . $pos
  28.           . '&session_max_rows=' . $session_max_rows
  29.           . '&disp_direction=' . $disp_direction
  30.           . '&repeat_cells=' . $repeat_cells
  31.           . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query));
  32. } else if ($goto == 'sql.php') {
  33.     $goto = 'sql.php?'
  34.           . 'lang=' . $lang
  35.           . '&server=' . $server
  36.           . '&db=' . urlencode($db)
  37.           . '&table=' . urlencode($table)
  38.           . '&pos=' . $pos
  39.           . '&session_max_rows=' . $session_max_rows
  40.           . '&disp_direction=' . $disp_direction
  41.           . '&repeat_cells=' . $repeat_cells
  42.           . '&sql_query=' . urlencode($sql_query);
  43. }
  44. // Defines the url to return in case of failure of the query
  45. if (isset($url_err)) {
  46.     $url_err = urldecode($url_err);
  47. } else {
  48.     $url_err = str_replace('&', '&', $goto);
  49. }
  50. // Resets tables defined in the configuration file
  51. reset($fields);
  52. if (isset($funcs)) {
  53.     reset($funcs);
  54. }
  55. // Misc
  56. if (get_magic_quotes_gpc()) {
  57.     $submit_type = stripslashes($submit_type);
  58. }
  59.  
  60.  
  61. /**
  62.  * Prepares the update of a row
  63.  */
  64. if (isset($primary_key) && ($submit_type != $strInsertAsNewRow)) {
  65.     // Restore the "primary key" to a convenient format
  66.     $primary_key = urldecode($primary_key);
  67.  
  68.     // Defines the SET part of the sql query
  69.     $valuelist = '';
  70.     while (list($key, $val) = each($fields)) {
  71.         $encoded_key = $key;
  72.         $key         = urldecode($key);
  73.  
  74.         switch (strtolower($val)) {
  75.             case 'null':
  76.                 break;
  77.             case '$enum$':
  78.                 // if we have an enum, then construct the value
  79.                 $f = 'field_' . md5($key);
  80.                 if (!empty($$f)) {
  81.                     $val     = implode(',', $$f);
  82.                     if ($val == 'null') {
  83.                         // void
  84.                     } else {
  85.                         $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  86.                     }
  87.                 } else {
  88.                     $val     = "''";
  89.                 }
  90.                 break;
  91.             case '$set$':
  92.                 // if we have a set, then construct the value
  93.                 $f = 'field_' . md5($key);
  94.                 if (!empty($$f)) {
  95.                     $val = implode(',', $$f);
  96.                     $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  97.                 } else {
  98.                     $val = "''";
  99.                 }
  100.                 break;
  101.             default:
  102.                 if (get_magic_quotes_gpc()) {
  103.                     $val = "'" . str_replace('\\"', '"', $val) . "'";
  104.                 } else {
  105.                     $val = "'" . PMA_sqlAddslashes($val) . "'";
  106.                 }
  107.                 break;
  108.         } // end switch
  109.  
  110.         // Was the Null checkbox checked for this field?
  111.         if (isset($fields_null) && isset($fields_null[$encoded_key])) {
  112.             $val = 'NULL';
  113.         }
  114.  
  115.         // No change for this column and no MySQL function is used -> next column
  116.         if (empty($funcs[$encoded_key])
  117.             && isset($fields_prev) && isset($fields_prev[$encoded_key])
  118.             && ("'" . PMA_sqlAddslashes(urldecode($fields_prev[$encoded_key])) . "'" == $val)) {
  119.             continue;
  120.         }
  121.         else if (!empty($val)) {
  122.             if (empty($funcs[$encoded_key])) {
  123.                 $valuelist .= PMA_backquote($key) . ' = ' . $val . ', ';
  124.             } else if ($val == '\'\''
  125.                        && (ereg('^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$', $funcs[$encoded_key]))) {
  126.                 $valuelist .= PMA_backquote($key) . ' = ' . $funcs[$encoded_key] . '(), ';
  127.             } else {
  128.                 $valuelist .= PMA_backquote($key) . ' = ' . $funcs[$encoded_key] . "($val), ";
  129.             }
  130.         }
  131.     } // end while
  132.  
  133.     // Builds the sql upate query
  134.     $valuelist    = ereg_replace(', $', '', $valuelist);
  135.     if (!empty($valuelist)) {
  136.         $query    = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key
  137.                   . ((PMA_MYSQL_INT_VERSION >= 32300) ? ' LIMIT 1' : '');
  138.         $message  = $strAffectedRows . ' ';
  139.     }
  140.     // No change -> move back to the calling script
  141.     else {
  142.         $message = $strNoModification;
  143.         if (@file_exists('./' . $goto)) {
  144.             $js_to_run = 'functions.js';
  145.             include('./header.inc.php');
  146.             include('./' . ereg_replace('\.\.*', '.', $goto));
  147.         } else {
  148.             header('Location: ' . $cfgPmaAbsoluteUri . $goto . '&message=' . urlencode($message));
  149.         }
  150.         exit();
  151.     }
  152. } // end row update
  153.  
  154.  
  155. /**
  156.  *  Prepares the insert of a row
  157.  */
  158. else {
  159.     $fieldlist = '';
  160.     $valuelist = '';
  161.     while (list($key, $val) = each($fields)) {
  162.         $encoded_key = $key;
  163.         $key         = urldecode($key);
  164.         $fieldlist   .= PMA_backquote($key) . ', ';
  165.  
  166.         switch (strtolower($val)) {
  167.             case 'null':
  168.                 break;
  169.             case '$enum$':
  170.                 // if we have a set, then construct the value
  171.                 $f = 'field_' . md5($key);
  172.                 if (!empty($$f)) {
  173.                     $val     = implode(',', $$f);
  174.                     if ($val == 'null') {
  175.                         // void
  176.                     } else {
  177.                         $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  178.                     }
  179.                 } else {
  180.                     $val     = "''";
  181.                 }
  182.                 break;
  183.             case '$set$':
  184.                 // if we have a set, then construct the value
  185.                 $f = 'field_' . md5($key);
  186.                 if (!empty($$f)) {
  187.                     $val = implode(',', $$f);
  188.                     $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  189.                 } else {
  190.                     $val = "''";
  191.                 }
  192.                 break;
  193.             default:
  194.                 if (get_magic_quotes_gpc()) {
  195.                     $val = "'" . str_replace('\\"', '"', $val) . "'";
  196.                 } else {
  197.                     $val = "'" . PMA_sqlAddslashes($val) . "'";
  198.                 }
  199.                 break;
  200.         } // end switch
  201.  
  202.         // Was the Null checkbox checked for this field?
  203.         if (isset($fields_null) && isset($fields_null[$encoded_key])) {
  204.             $val = 'NULL';
  205.         }
  206.  
  207.         if (empty($funcs[$encoded_key])) {
  208.             $valuelist .= $val . ', ';
  209.         } else if ($val == '\'\''
  210.                    && (ereg('^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$', $funcs[$encoded_key]))) {
  211.             $valuelist .= $funcs[$encoded_key] . '(), ';
  212.         } else {
  213.             $valuelist .= $funcs[$encoded_key] . '(' . $val . '), ';
  214.         }
  215.     } // end while
  216.  
  217.     // Builds the sql insert query
  218.     $fieldlist = ereg_replace(', $', '', $fieldlist);
  219.     $valuelist = ereg_replace(', $', '', $valuelist);
  220.     $query     = 'INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES (' . $valuelist . ')';
  221.     $message   = $strInsertedRows . ' ';
  222. } // end row insertion
  223.  
  224.  
  225. /**
  226.  * Executes the sql query and get the result, then move back to the calling
  227.  * page
  228.  */
  229. mysql_select_db($db);
  230. $sql_query = $query . ';';
  231. $result    = mysql_query($query);
  232.  
  233. if (!$result) {
  234.     $error = mysql_error();
  235.     include('./header.inc.php');
  236.     PMA_mysqlDie($error, '', '', $url_err);
  237. } else {
  238.     if (@mysql_affected_rows()) {
  239.         $message .= @mysql_affected_rows();
  240.     } else {
  241.         $message = $strModifications;
  242.     }
  243.     if (@file_exists('./' . $goto)) {
  244.         if ($goto == 'db_details.php' && !empty($table)) {
  245.             unset($table);
  246.         }
  247.         $js_to_run = 'functions.js';
  248.         include('./header.inc.php');
  249.         include('./' . ereg_replace('\.\.*', '.', $goto));
  250.     } else {
  251.         $add_query = (strpos(' ' . $goto, 'tbl_change') ? '&disp_query=' . urlencode($sql_query) : '');
  252.         header('Location: ' . $cfgPmaAbsoluteUri . $goto . '&message=' . urlencode($message) . $add_query);
  253.     }
  254.     exit();
  255. } // end if
  256. ?>
  257.