saving of theme and font size
[phpmyadmin.git] / tbl_replace.php
blobb508342ca31deef896af55bf99d6c810b0483ee4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * manipulation of table data like inserting, replacing and updating
6 * usally called as form action from tbl_change.php to insert or update table rows
8 * @version $Id$
10 * @todo 'edit_next' tends to not work as expected if used ... at least there is no order by
11 * it needs the original query and the row number and than replace the LIMIT clause
12 * @uses PMA_checkParameters()
13 * @uses PMA_DBI_select_db()
14 * @uses PMA_DBI_query()
15 * @uses PMA_DBI_fetch_row()
16 * @uses PMA_DBI_get_fields_meta()
17 * @uses PMA_DBI_free_result()
18 * @uses PMA_DBI_try_query()
19 * @uses PMA_DBI_getError()
20 * @uses PMA_DBI_affected_rows()
21 * @uses PMA_DBI_insert_id()
22 * @uses PMA_backquote()
23 * @uses PMA_getUniqueCondition()
24 * @uses PMA_sqlAddslashes()
25 * @uses PMA_securePath()
26 * @uses PMA_sendHeaderLocation()
27 * @uses str_replace()
28 * @uses count()
29 * @uses file_exists()
30 * @uses strlen()
31 * @uses str_replace()
32 * @uses preg_replace()
33 * @uses is_array()
34 * @uses $GLOBALS['db']
35 * @uses $GLOBALS['table']
36 * @uses $GLOBALS['goto']
37 * @uses $GLOBALS['sql_query']
38 * @uses PMA_File::getRecentBLOBReference()
39 * @package phpMyAdmin
42 /**
43 * do not import request variable into global scope
45 * cannot be used as long as it could happen that the $goto file that is included
46 * at the end of this script is not updated to work without imported request variables
48 * @todo uncomment this if all possible included files to rely on import request variables
49 if (! defined('PMA_NO_VARIABLES_IMPORT')) {
50 define('PMA_NO_VARIABLES_IMPORT', true);
53 /**
54 * Gets some core libraries
56 require_once './libraries/common.inc.php';
57 $blob_streaming_active = $GLOBALS['PMA_Config']->get('BLOBSTREAMING_PLUGINS_EXIST');
59 // Check parameters
60 PMA_checkParameters(array('db', 'table', 'goto'));
62 PMA_DBI_select_db($GLOBALS['db']);
64 /**
65 * Initializes some variables
67 $goto_include = false;
69 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
70 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
71 $GLOBALS['js_include'][] = 'tbl_change.js';
72 require_once './libraries/header.inc.php';
73 require './tbl_change.php';
74 exit;
77 if (isset($_REQUEST['after_insert'])
78 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
79 $url_params['after_insert'] = $_REQUEST['after_insert'];
80 //$GLOBALS['goto'] = 'tbl_change.php';
81 $goto_include = 'tbl_change.php';
83 if (isset($_REQUEST['where_clause'])) {
84 if ($_REQUEST['after_insert'] == 'same_insert') {
85 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
86 $url_params['where_clause'][] = $one_where_clause;
88 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
89 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
90 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
91 . ' WHERE ' . str_replace('` =', '` >', $one_where_clause)
92 . ' LIMIT 1;';
93 $res = PMA_DBI_query($local_query);
94 $row = PMA_DBI_fetch_row($res);
95 $meta = PMA_DBI_get_fields_meta($res);
96 // must find a unique condition based on unique key,
97 // not a combination of all fields
98 list($unique_condition, $clause_is_unique) = PMA_getUniqueCondition($res, count($meta), $meta, $row, true);
99 if (! empty($unique_condition)) {
100 $_SESSION['edit_next'] = $unique_condition;
102 unset($unique_condition, $clause_is_unique);
106 } elseif (! empty($GLOBALS['goto'])) {
107 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
108 // this should NOT happen
109 //$GLOBALS['goto'] = false;
110 $goto_include = false;
111 } else {
112 $goto_include = $GLOBALS['goto'];
114 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
115 $GLOBALS['table'] = '';
119 if (! $goto_include) {
120 if (! strlen($GLOBALS['table'])) {
121 $goto_include = 'db_sql.php';
122 } else {
123 $goto_include = 'tbl_sql.php';
127 // Defines the url to return in case of failure of the query
128 if (isset($_REQUEST['err_url'])) {
129 $err_url = $_REQUEST['err_url'];
130 } else {
131 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
135 * Prepares the update/insert of a row
137 if (isset($_REQUEST['where_clause'])) {
138 // we were editing something => use the WHERE clause
139 $loop_array = (is_array($_REQUEST['where_clause']) ? $_REQUEST['where_clause'] : array($_REQUEST['where_clause']));
140 $using_key = true;
141 $is_insert = ($_REQUEST['submit_type'] == 'insert') || ($_REQUEST['submit_type'] == 'showinsert') || ($_REQUEST['submit_type'] == 'insertignore');
142 $is_insertignore = ($_REQUEST['submit_type'] == 'insertignore');
143 } else {
144 // new row => use indexes
145 $loop_array = array();
146 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
147 $loop_array[] = $key;
149 $using_key = false;
150 $is_insert = true;
151 $is_insertignore = false;
154 $query = array();
155 $value_sets = array();
156 $func_no_param = array(
157 'NOW',
158 'CURDATE',
159 'CURTIME',
160 'UTC_DATE',
161 'UTC_TIME',
162 'UTC_TIMESTAMP',
163 'UNIX_TIMESTAMP',
164 'RAND',
165 'USER',
166 'LAST_INSERT_ID',
167 'UUID',
168 'CURRENT_USER',
171 foreach ($loop_array as $rowcount => $where_clause) {
172 // skip fields to be ignored
173 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
174 continue;
177 // Defines the SET part of the sql query
178 $query_values = array();
180 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
181 $me_fields =
182 isset($_REQUEST['fields']['multi_edit'][$rowcount])
183 ? $_REQUEST['fields']['multi_edit'][$rowcount]
184 : array();
185 $me_fields_name =
186 isset($_REQUEST['fields_name']['multi_edit'][$rowcount])
187 ? $_REQUEST['fields_name']['multi_edit'][$rowcount]
188 : null;
189 $me_fields_prev =
190 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
191 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
192 : null;
193 $me_funcs =
194 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
195 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
196 : null;
197 $me_fields_type =
198 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
199 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
200 : null;
201 $me_fields_null =
202 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
203 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
204 : null;
205 $me_fields_null_prev =
206 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
207 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
208 : null;
209 $me_auto_increment =
210 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
211 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
212 : null;
214 if ($blob_streaming_active) {
215 $primary_field = PMA_BS_GetPrimaryField($GLOBALS['db'], $GLOBALS['table']);
218 // Fetch the current values of a row to use in case we have a protected field
219 // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
220 if ($is_insert && $using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($where_clause)) {
221 $prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';');
223 foreach ($me_fields as $key => $val) {
225 // Note: $key is an md5 of the fieldname. The actual fieldname is available in $me_fields_name[$key]
227 require './libraries/tbl_replace_fields.inc.php';
229 // for blobstreaming
230 if ($blob_streaming_active && (NULL != $primary_field || strlen($primary_field) > 0)) {
231 $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ? $_REQUEST['remove_blob_repo_' . $key] : NULL;
232 $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ? $_REQUEST['upload_blob_repo_' . $key] : NULL;
234 // checks if an existing blob repository reference should be removed
235 if (isset($remove_blob_repo) && !isset($upload_blob_repo)) {
236 $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
237 if (isset($remove_blob_reference)) {
238 $val = "''";
241 // checks if this field requires a bs reference attached to it
242 if (isset($upload_blob_repo)) {
243 // get the most recent BLOB reference
244 $bs_reference = PMA_File::getRecentBLOBReference();
246 // if the most recent BLOB reference exists, set it as a field value
247 if (!is_null($bs_reference)) {
248 $val = "'" . PMA_sqlAddslashes($bs_reference) . "'";
254 if (empty($me_funcs[$key])) {
255 $cur_value = $val;
256 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
257 $cur_value = $me_funcs[$key] . '(' . $val . ')';
258 } elseif ('UUID' === $me_funcs[$key]) {
259 /* This way user will know what UUID new row has */
260 $uuid = PMA_DBI_fetch_value('SELECT UUID()');
261 $cur_value = "'" . $uuid . "'";
262 } elseif (in_array($me_funcs[$key], $func_no_param)) {
263 $cur_value = $me_funcs[$key] . '()';
264 } else {
265 $cur_value = $me_funcs[$key] . '(' . $val . ')';
268 // i n s e r t
269 if ($is_insert) {
270 // no need to add column into the valuelist
271 if (strlen($cur_value)) {
272 $query_values[] = $cur_value;
273 // first inserted row so prepare the list of fields
274 if (empty($value_sets)) {
275 $query_fields[] = PMA_backquote($me_fields_name[$key]);
279 // u p d a t e
280 } elseif (!empty($me_fields_null_prev[$key])
281 && !isset($me_fields_null[$key])) {
282 // field had the null checkbox before the update
283 // field no longer has the null checkbox
284 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
285 } elseif (empty($me_funcs[$key])
286 && isset($me_fields_prev[$key])
287 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
288 // No change for this column and no MySQL function is used -> next column
289 continue;
290 } elseif (! empty($val)) {
291 // avoid setting a field to NULL when it's already NULL
292 // (field had the null checkbox before the update
293 // field still has the null checkbox)
294 if (!(! empty($me_fields_null_prev[$key])
295 && isset($me_fields_null[$key]))) {
296 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
299 } // end foreach ($me_fields as $key => $val)
301 if (count($query_values) > 0) {
302 if ($is_insert) {
303 $value_sets[] = implode(', ', $query_values);
304 } else {
305 // build update query
306 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
307 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $where_clause . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1');
311 } // end foreach ($loop_array as $where_clause)
312 unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
313 $me_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, $using_key,
314 $func_no_param);
317 // Builds the sql query
318 if ($is_insert && count($value_sets) > 0) {
319 if ($is_insertignore) {
320 $insert_command = 'INSERT IGNORE ';
321 } else {
322 $insert_command = 'INSERT ';
324 $query[] = $insert_command . 'INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
325 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
326 unset($insert_command);
328 unset($query_fields);
329 } elseif (empty($query)) {
330 // No change -> move back to the calling script
331 $message = PMA_Message::success(__('No change'));
332 $GLOBALS['js_include'][] = 'functions.js';
333 $active_page = $goto_include;
334 require_once './libraries/header.inc.php';
335 require './' . PMA_securePath($goto_include);
336 exit;
338 unset($me_fields, $is_insertignore);
341 * Executes the sql query and get the result, then move back to the calling
342 * page
344 if (! empty($GLOBALS['sql_query'])) {
345 $url_params['sql_query'] = $GLOBALS['sql_query'];
346 $return_to_sql_query = $GLOBALS['sql_query'];
348 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
349 // to ensure that the query is displayed in case of
350 // "insert as new row" and then "insert another new row"
351 $GLOBALS['display_query'] = $GLOBALS['sql_query'];
353 $total_affected_rows = 0;
354 $last_messages = array();
355 $warning_messages = array();
356 $error_messages = array();
358 foreach ($query as $single_query) {
359 if ($_REQUEST['submit_type'] == 'showinsert') {
360 $last_messages[] = PMA_Message::notice(__('Showing SQL query'));
361 continue;
363 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
364 $result = PMA_DBI_try_query($single_query);
365 } else {
366 $result = PMA_DBI_query($single_query);
369 if (! $result) {
370 $error_messages[] = PMA_DBI_getError();
371 } else {
372 // the following is a real assignment:
373 if ($tmp = @PMA_DBI_affected_rows()) {
374 $total_affected_rows += $tmp;
376 unset($tmp);
378 $insert_id = PMA_DBI_insert_id();
379 if ($insert_id != 0) {
380 // insert_id is id of FIRST record inserted in one insert, so if we
381 // inserted multiple rows, we had to increment this
383 if ($total_affected_rows > 0) {
384 $insert_id = $insert_id + $total_affected_rows - 1;
386 $last_message = PMA_Message::notice(__('Inserted row id: %1$d'));
387 $last_message->addParam($insert_id);
388 $last_messages[] = $last_message;
390 PMA_DBI_free_result($result);
391 } // end if
393 foreach (PMA_DBI_get_warnings() as $warning) {
394 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
395 . ' ' . $warning['Message'];
398 unset($result);
400 unset($single_query, $query);
402 if ($is_insert && count($value_sets) > 0) {
403 $message = PMA_Message::inserted_rows($total_affected_rows);
404 } else {
405 $message = PMA_Message::affected_rows($total_affected_rows);
408 $message->addMessages($last_messages, '<br />');
410 if (! empty($warning_messages)) {
412 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
413 * the message
415 $message->addMessages($warning_messages, '<br />');
416 $message->isWarning(true);
418 if (! empty($error_messages)) {
419 $message->addMessages($error_messages);
420 $message->isError(true);
422 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
424 if (isset($return_to_sql_query)) {
425 $disp_query = $GLOBALS['sql_query'];
426 $disp_message = $message;
427 unset($message);
428 $GLOBALS['sql_query'] = $return_to_sql_query;
431 $GLOBALS['js_include'][] = 'tbl_change.js';
432 $GLOBALS['js_include'][] = 'functions.js';
433 // in case we call sql.php which needs those:
434 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
436 $active_page = $goto_include;
439 * If user asked for "and then Insert another new row" we have to remove
440 * WHERE clause information so that tbl_change.php does not go back
441 * to the current record
443 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
444 unset($_REQUEST['where_clause']);
448 * Load header.
450 require_once './libraries/header.inc.php';
452 * Load target page.
454 require './' . PMA_securePath($goto_include);
455 exit;