3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Gets some core libraries
9 require_once('./libraries/grab_globals.lib.php');
10 require_once('./libraries/common.lib.php');
13 PMA_checkParameters(array('db','table','goto'));
16 * Initializes some variables
18 // Defines the url to return in case of success of the query
19 if (isset($sql_query)) {
20 $sql_query = urldecode($sql_query);
22 if (!isset($dontlimitchars)) {
26 if (isset($after_insert) && $after_insert == 'new_insert') {
27 $goto = 'tbl_change.php?'
28 . PMA_generate_common_url($db, $table, '&')
29 . '&goto=' . urlencode($goto)
31 . '&session_max_rows=' . $session_max_rows
32 . '&disp_direction=' . $disp_direction
33 . '&repeat_cells=' . $repeat_cells
34 . '&dontlimitchars=' . $dontlimitchars
35 . (empty($sql_query) ?
'' : '&sql_query=' . urlencode($sql_query));
36 } else if ($goto == 'sql.php') {
38 . PMA_generate_common_url($db, $table, '&')
40 . '&session_max_rows=' . $session_max_rows
41 . '&disp_direction=' . $disp_direction
42 . '&repeat_cells=' . $repeat_cells
43 . '&dontlimitchars=' . $dontlimitchars
44 . '&sql_query=' . urlencode($sql_query);
45 } else if (!empty($goto)) {
47 $is_gotofile = preg_replace('@^([^?]+).*$@', '\\1', $goto);
48 if (!@file_exists
('./' . $is_gotofile)) {
49 $goto = (empty($table)) ?
'db_details.php' : 'tbl_properties.php';
52 $is_gotofile = ($is_gotofile == $goto);
56 // Defines the url to return in case of failure of the query
57 if (isset($err_url)) {
58 $err_url = urldecode($err_url);
60 $err_url = str_replace('&', '&', $goto)
61 . (empty($primary_key) ?
'' : '&primary_key=' . (is_array($primary_key) ?
$primary_key[0] : $primary_key));
64 // Resets tables defined in the configuration file
73 * Prepares the update of a row
75 if (isset($primary_key) && ($submit_type != $strInsertAsNewRow)) {
76 $loop_array = (is_array($primary_key) ?
$primary_key : array(0 => $primary_key));
77 PMA_mysql_select_db($db);
81 foreach($loop_array AS $primary_key_index => $enc_primary_key) {
82 // Restore the "primary key" to a convenient format
83 $primary_key = urldecode($enc_primary_key);
85 // Defines the SET part of the sql query
88 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
89 $me_fields = (isset($fields['multi_edit']) && isset($fields['multi_edit'][$enc_primary_key]) ?
$fields['multi_edit'][$enc_primary_key] : (isset($fields) ?
$fields : null));
90 $me_fields_prev = (isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ?
$fields_prev['multi_edit'][$enc_primary_key] : (isset($fields_prev) ?
$fields_prev : null));
91 $me_funcs = (isset($funcs['multi_edit']) && isset($funcs['multi_edit'][$enc_primary_key]) ?
$funcs['multi_edit'][$enc_primary_key] : (isset($funcs) ?
$funcs : null));
92 $me_fields_type = (isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ?
$fields_type['multi_edit'][$enc_primary_key] : (isset($fields_type) ?
$fields_type : null));
93 $me_fields_null = (isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ?
$fields_null['multi_edit'][$enc_primary_key] : (isset($fields_null) ?
$fields_null : null));
95 foreach($me_fields AS $key => $val) {
97 $key = urldecode($key);
99 require('./tbl_replace_fields.php');
101 // No change for this column and no MySQL function is used -> next column
102 if (empty($me_funcs[$encoded_key])
103 && isset($me_fields_prev) && isset($me_fields_prev[$encoded_key])
104 && ("'" . PMA_sqlAddslashes(urldecode($me_fields_prev[$encoded_key])) . "'" == $val)) {
107 else if (!empty($val)) {
108 if (empty($me_funcs[$encoded_key])) {
109 $valuelist .= PMA_backquote($key) . ' = ' . $val . ', ';
110 } else if ($val == '\'\''
111 && (preg_match('@^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$@', $me_funcs[$encoded_key]))) {
112 $valuelist .= PMA_backquote($key) . ' = ' . $me_funcs[$encoded_key] . '(), ';
114 $valuelist .= PMA_backquote($key) . ' = ' . $me_funcs[$encoded_key] . "($val), ";
119 // Builds the sql update query
120 $valuelist = preg_replace('@, $@', '', $valuelist);
121 if (!empty($valuelist)) {
122 $query[] = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key
125 // lem9: why a line break here?
126 //$message = $strAffectedRows . ' <br />';
127 $message = $strAffectedRows . ' ';
131 if (empty($valuelist)) {
132 // No change -> move back to the calling script
133 $message = $strNoModification;
135 $js_to_run = 'functions.js';
136 require_once('./header.inc.php');
137 require('./' . preg_replace('@\.\.*@', '.', $goto));
139 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . '&disp_query=');
147 * Prepares the insert of a row
150 $loop_array = (isset($primary_key) && is_array($primary_key) ?
$primary_key : array(0 => (isset($primary_key) ?
$primary_key : null)));
153 PMA_mysql_select_db($db);
155 foreach($loop_array AS $primary_key_index => $enc_primary_key) {
159 $me_fields = (isset($fields['multi_edit']) && isset($fields['multi_edit'][$enc_primary_key]) ?
$fields['multi_edit'][$enc_primary_key] : (isset($fields) ?
$fields : null));
160 $me_fields_prev = (isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ?
$fields_prev['multi_edit'][$enc_primary_key] : (isset($fields_prev) ?
$fields_prev : null));
161 $me_funcs = (isset($funcs['multi_edit']) && isset($funcs['multi_edit'][$enc_primary_key]) ?
$funcs['multi_edit'][$enc_primary_key] : (isset($funcs) ?
$funcs : null));
162 $me_fields_type = (isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ?
$fields_type['multi_edit'][$enc_primary_key] : (isset($fields_type) ?
$fields_type : null));
163 $me_fields_null = (isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ?
$fields_null['multi_edit'][$enc_primary_key] : (isset($fields_null) ?
$fields_null : null));
165 // garvin: Get, if sent, any protected fields to insert them here:
166 if (isset($me_fields_type) && is_array($me_fields_type) && isset($enc_primary_key)) {
167 $prot_local_query = 'SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . urldecode($enc_primary_key);
168 $prot_result = PMA_mysql_query($prot_local_query) or PMA_mysqlDie('', $prot_local_query, '', $err_url);
169 $prot_row = PMA_mysql_fetch_array($prot_result);
172 foreach($me_fields AS $key => $val) {
174 $key = urldecode($key);
175 $fieldlist .= PMA_backquote($key) . ', ';
177 require('./tbl_replace_fields.php');
179 if (empty($me_funcs[$encoded_key])) {
180 $valuelist .= $val . ', ';
181 } else if (($val == '\'\''
182 && preg_match('@^(UNIX_TIMESTAMP|RAND|LAST_INSERT_ID)$@', $me_funcs[$encoded_key]))
183 ||
preg_match('@^(NOW|CURDATE|CURTIME|USER)$@', $me_funcs[$encoded_key])) {
184 $valuelist .= $me_funcs[$encoded_key] . '(), ';
186 $valuelist .= $me_funcs[$encoded_key] . '(' . $val . '), ';
190 // Builds the sql insert query
191 $fieldlist = preg_replace('@, $@', '', $fieldlist);
192 $valuelist = preg_replace('@, $@', '', $valuelist);
193 $query[] = 'INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES (' . $valuelist . ')';
194 $message = $strInsertedRows . ' ';
196 } // end row insertion
200 * Executes the sql query and get the result, then move back to the calling
203 $sql_query = implode(';', $query) . ';';
204 $total_affected_rows = 0;
207 foreach($query AS $query_index => $single_query) {
208 $result = PMA_mysql_query($single_query);
210 if ($cfg['IgnoreMultiSubmitErrors']) {
211 $message .= PMA_mysql_error();
213 $error = PMA_mysql_error();
214 require_once('./header.inc.php');
215 PMA_mysqlDie($error, '', '', $err_url);
218 if (@mysql_affected_rows
()) {
219 $total_affected_rows +
= @mysql_affected_rows
();
222 $insert_id = mysql_insert_id();
223 if ($insert_id != 0) {
224 $last_message .= '<br />'.$strInsertedRowId . ' ' . $insert_id;
229 if ($total_affected_rows != 0) {
230 //$message .= '<br />' . $total_affected_rows;
231 $message .= $total_affected_rows;
233 $message .= $strModifications;
236 $message .= $last_message;
239 if ($goto == 'db_details.php' && !empty($table)) {
242 $js_to_run = 'functions.js';
243 $active_page = $goto;
244 require_once('./header.inc.php');
245 require('./' . preg_replace('@\.\.*@', '.', $goto));
247 // I don't understand this one:
248 //$add_query = (strpos(' ' . $goto, 'tbl_change') ? '&disp_query=' . urlencode($sql_query) : '');
250 // if we have seen binary,
251 // we do not append the query to the Location so it won't be displayed
252 // on the resulting page
253 // Nijel: we also need to limit size of url...
254 $add_query = (!$seen_binary && strlen($sql_query) < 1024 ?
'&disp_query=' . urlencode($sql_query) : '');
255 header('Location: ' . $cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . $add_query);