2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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
9 * @todo 'edit_next' tends to not work as expected if used ... at least there is no order by
10 * it needs the original query and the row number and than replace the LIMIT clause
11 * @uses PMA_checkParameters()
12 * @uses PMA_DBI_select_db()
13 * @uses PMA_DBI_query()
14 * @uses PMA_DBI_fetch_row()
15 * @uses PMA_DBI_get_fields_meta()
16 * @uses PMA_DBI_free_result()
17 * @uses PMA_DBI_try_query()
18 * @uses PMA_DBI_getError()
19 * @uses PMA_DBI_affected_rows()
20 * @uses PMA_DBI_insert_id()
21 * @uses PMA_backquote()
22 * @uses PMA_getUniqueCondition()
23 * @uses PMA_sqlAddslashes()
24 * @uses PMA_securePath()
25 * @uses PMA_sendHeaderLocation()
31 * @uses preg_replace()
33 * @uses $GLOBALS['db']
34 * @uses $GLOBALS['table']
35 * @uses $GLOBALS['goto']
36 * @uses $GLOBALS['sql_query']
37 * @uses PMA_File::getRecentBLOBReference()
42 * do not import request variable into global scope
44 * cannot be used as long as it could happen that the $goto file that is included
45 * at the end of this script is not updated to work without imported request variables
47 * @todo uncomment this if all possible included files to rely on import request variables
48 if (! defined('PMA_NO_VARIABLES_IMPORT')) {
49 define('PMA_NO_VARIABLES_IMPORT', true);
53 * Gets some core libraries
55 require_once './libraries/common.inc.php';
56 $blob_streaming_active = $GLOBALS['PMA_Config']->get('BLOBSTREAMING_PLUGINS_EXIST');
59 PMA_checkParameters(array('db', 'table', 'goto'));
61 PMA_DBI_select_db($GLOBALS['db']);
64 * Initializes some variables
66 $goto_include = false;
68 $GLOBALS['js_include'][] = 'makegrid.js';
69 // Needed for generation of Inline Edit anchors
70 $GLOBALS['js_include'][] = 'sql.js';
72 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
73 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
74 $GLOBALS['js_include'][] = 'tbl_change.js';
75 require_once './libraries/header.inc.php';
76 require './tbl_change.php';
80 if (isset($_REQUEST['after_insert'])
81 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
82 $url_params['after_insert'] = $_REQUEST['after_insert'];
83 //$GLOBALS['goto'] = 'tbl_change.php';
84 $goto_include = 'tbl_change.php';
86 if (isset($_REQUEST['where_clause'])) {
87 if ($_REQUEST['after_insert'] == 'same_insert') {
88 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
89 $url_params['where_clause'][] = $one_where_clause;
91 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
92 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
93 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
94 . ' WHERE ' . str_replace('` =', '` >', $one_where_clause)
96 $res = PMA_DBI_query($local_query);
97 $row = PMA_DBI_fetch_row($res);
98 $meta = PMA_DBI_get_fields_meta($res);
99 // must find a unique condition based on unique key,
100 // not a combination of all fields
101 list($unique_condition, $clause_is_unique) = PMA_getUniqueCondition($res, count($meta), $meta, $row, true);
102 if (! empty($unique_condition)) {
103 $_SESSION['edit_next'] = $unique_condition;
105 unset($unique_condition, $clause_is_unique);
109 } elseif (! empty($GLOBALS['goto'])) {
110 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
111 // this should NOT happen
112 //$GLOBALS['goto'] = false;
113 $goto_include = false;
115 $goto_include = $GLOBALS['goto'];
117 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
118 $GLOBALS['table'] = '';
122 if (! $goto_include) {
123 if (! strlen($GLOBALS['table'])) {
124 $goto_include = 'db_sql.php';
126 $goto_include = 'tbl_sql.php';
130 // Defines the url to return in case of failure of the query
131 if (isset($_REQUEST['err_url'])) {
132 $err_url = $_REQUEST['err_url'];
134 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
138 * Prepares the update/insert of a row
140 if (isset($_REQUEST['where_clause'])) {
141 // we were editing something => use the WHERE clause
142 $loop_array = (is_array($_REQUEST['where_clause']) ?
$_REQUEST['where_clause'] : array($_REQUEST['where_clause']));
144 $is_insert = ($_REQUEST['submit_type'] == 'insert') ||
($_REQUEST['submit_type'] == 'showinsert') ||
($_REQUEST['submit_type'] == 'insertignore');
145 $is_insertignore = ($_REQUEST['submit_type'] == 'insertignore');
147 // new row => use indexes
148 $loop_array = array();
149 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
150 $loop_array[] = $key;
154 $is_insertignore = false;
158 $value_sets = array();
159 $func_no_param = array(
174 foreach ($loop_array as $rownumber => $where_clause) {
175 // skip fields to be ignored
176 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
180 // Defines the SET part of the sql query
181 $query_values = array();
183 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
185 isset($_REQUEST['fields']['multi_edit'][$rownumber])
186 ?
$_REQUEST['fields']['multi_edit'][$rownumber]
189 isset($_REQUEST['fields_name']['multi_edit'][$rownumber])
190 ?
$_REQUEST['fields_name']['multi_edit'][$rownumber]
193 isset($_REQUEST['fields_prev']['multi_edit'][$rownumber])
194 ?
$_REQUEST['fields_prev']['multi_edit'][$rownumber]
197 isset($_REQUEST['funcs']['multi_edit'][$rownumber])
198 ?
$_REQUEST['funcs']['multi_edit'][$rownumber]
201 isset($_REQUEST['fields_type']['multi_edit'][$rownumber])
202 ?
$_REQUEST['fields_type']['multi_edit'][$rownumber]
205 isset($_REQUEST['fields_null']['multi_edit'][$rownumber])
206 ?
$_REQUEST['fields_null']['multi_edit'][$rownumber]
208 $me_fields_null_prev =
209 isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber])
210 ?
$_REQUEST['fields_null_prev']['multi_edit'][$rownumber]
213 isset($_REQUEST['auto_increment']['multi_edit'][$rownumber])
214 ?
$_REQUEST['auto_increment']['multi_edit'][$rownumber]
217 // Fetch the current values of a row to use in case we have a protected field
218 // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
219 if ($is_insert && $using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($where_clause)) {
220 $prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';');
222 foreach ($me_fields as $key => $val) {
224 // Note: $key is an md5 of the fieldname. The actual fieldname is available in $me_fields_name[$key]
226 require './libraries/tbl_replace_fields.inc.php';
229 if ($blob_streaming_active) {
230 $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ?
$_REQUEST['remove_blob_repo_' . $key] : NULL;
231 $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ?
$_REQUEST['upload_blob_repo_' . $key] : NULL;
233 // checks if an existing blob repository reference should be removed
234 if (isset($remove_blob_repo) && ! isset($upload_blob_repo)) {
235 $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
236 if (isset($remove_blob_reference)) {
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) . "'";
253 if (empty($me_funcs[$key])) {
255 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
256 $cur_value = $me_funcs[$key] . '(' . $val . ')';
257 } elseif ('UUID' === $me_funcs[$key]) {
258 /* This way user will know what UUID new row has */
259 $uuid = PMA_DBI_fetch_value('SELECT UUID()');
260 $cur_value = "'" . $uuid . "'";
261 } elseif (in_array($me_funcs[$key], $func_no_param)) {
262 $cur_value = $me_funcs[$key] . '()';
264 $cur_value = $me_funcs[$key] . '(' . $val . ')';
269 // no need to add column into the valuelist
270 if (strlen($cur_value)) {
271 $query_values[] = $cur_value;
272 // first inserted row so prepare the list of fields
273 if (empty($value_sets)) {
274 $query_fields[] = PMA_backquote($me_fields_name[$key]);
279 } elseif (!empty($me_fields_null_prev[$key])
280 && ! isset($me_fields_null[$key])) {
281 // field had the null checkbox before the update
282 // field no longer has the null checkbox
283 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
284 } elseif (empty($me_funcs[$key])
285 && isset($me_fields_prev[$key])
286 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
287 // No change for this column and no MySQL function is used -> next column
289 } elseif (! empty($val)) {
290 // avoid setting a field to NULL when it's already NULL
291 // (field had the null checkbox before the update
292 // field still has the null checkbox)
293 if (!(! empty($me_fields_null_prev[$key])
294 && isset($me_fields_null[$key]))) {
295 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
298 } // end foreach ($me_fields as $key => $val)
300 if (count($query_values) > 0) {
302 $value_sets[] = implode(', ', $query_values);
304 // build update query
305 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
306 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $where_clause . ($_REQUEST['clause_is_unique'] ?
'' : ' LIMIT 1');
310 } // end foreach ($loop_array as $where_clause)
311 unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
312 $me_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, $using_key,
316 // Builds the sql query
317 if ($is_insert && count($value_sets) > 0) {
318 if ($is_insertignore) {
319 $insert_command = 'INSERT IGNORE ';
321 $insert_command = 'INSERT ';
323 $query[] = $insert_command . 'INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
324 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
325 unset($insert_command);
327 unset($query_fields);
328 } elseif (empty($query)) {
329 // No change -> move back to the calling script
331 // Note: logic passes here for inline edit
332 $message = PMA_Message
::success(__('No change'));
333 $active_page = $goto_include;
334 if(! $GLOBALS['is_ajax_request'] == true) {
335 require_once './libraries/header.inc.php';
337 require './' . PMA_securePath($goto_include);
340 unset($me_fields, $is_insertignore);
343 * Executes the sql query and get the result, then move back to the calling
346 if (! empty($GLOBALS['sql_query'])) {
347 $url_params['sql_query'] = $GLOBALS['sql_query'];
348 $return_to_sql_query = $GLOBALS['sql_query'];
350 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
351 // to ensure that the query is displayed in case of
352 // "insert as new row" and then "insert another new row"
353 $GLOBALS['display_query'] = $GLOBALS['sql_query'];
355 $total_affected_rows = 0;
356 $last_messages = array();
357 $warning_messages = array();
358 $error_messages = array();
360 foreach ($query as $single_query) {
361 if ($_REQUEST['submit_type'] == 'showinsert') {
362 $last_messages[] = PMA_Message
::notice(__('Showing SQL query'));
365 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
366 $result = PMA_DBI_try_query($single_query);
368 $result = PMA_DBI_query($single_query);
372 $error_messages[] = PMA_DBI_getError();
374 // The next line contains a real assignment, it's not a typo
375 if ($tmp = @PMA_DBI_affected_rows
()) {
376 $total_affected_rows +
= $tmp;
380 $insert_id = PMA_DBI_insert_id();
381 if ($insert_id != 0) {
382 // insert_id is id of FIRST record inserted in one insert, so if we
383 // inserted multiple rows, we had to increment this
385 if ($total_affected_rows > 0) {
386 $insert_id = $insert_id +
$total_affected_rows - 1;
388 $last_message = PMA_Message
::notice(__('Inserted row id: %1$d'));
389 $last_message->addParam($insert_id);
390 $last_messages[] = $last_message;
392 PMA_DBI_free_result($result);
395 foreach (PMA_DBI_get_warnings() as $warning) {
396 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
397 . ' ' . $warning['Message'];
402 unset($single_query, $query);
404 if ($is_insert && count($value_sets) > 0) {
405 $message = PMA_Message
::inserted_rows($total_affected_rows);
407 $message = PMA_Message
::affected_rows($total_affected_rows);
410 $message->addMessages($last_messages, '<br />');
412 if (! empty($warning_messages)) {
414 * @todo use a <div class="error"> in PMA_showMessage() for this part of
417 $message->addMessages($warning_messages, '<br />');
418 $message->isError(true);
420 if (! empty($error_messages)) {
421 $message->addMessages($error_messages);
422 $message->isError(true);
424 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
426 if($GLOBALS['is_ajax_request'] == true) {
427 /**Get the total row count of the table*/
428 $extra_data['row_count'] = PMA_Table
::countRecords($_REQUEST['db'],$_REQUEST['table']);
429 $extra_data['sql_query'] = PMA_showMessage(NULL, $GLOBALS['display_query']);
430 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
433 if (isset($return_to_sql_query)) {
434 $disp_query = $GLOBALS['sql_query'];
435 $disp_message = $message;
437 $GLOBALS['sql_query'] = $return_to_sql_query;
440 $GLOBALS['js_include'][] = 'tbl_change.js';
441 // in case we call sql.php which needs those:
442 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
444 $active_page = $goto_include;
447 * If user asked for "and then Insert another new row" we have to remove
448 * WHERE clause information so that tbl_change.php does not go back
449 * to the current record
451 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
452 unset($_REQUEST['where_clause']);
458 require_once './libraries/header.inc.php';
462 require './' . PMA_securePath($goto_include);