Translation update done using Pootle.
[phpmyadmin/dkf.git] / tbl_replace.php
blobe372bd0abe42c90c75314976eb904f9b56b8ef80
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
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()
26 * @uses str_replace()
27 * @uses count()
28 * @uses file_exists()
29 * @uses strlen()
30 * @uses str_replace()
31 * @uses preg_replace()
32 * @uses is_array()
33 * @uses $GLOBALS['db']
34 * @uses $GLOBALS['table']
35 * @uses $GLOBALS['goto']
36 * @uses $GLOBALS['sql_query']
37 * @uses PMA_File::getRecentBLOBReference()
38 * @package phpMyAdmin
41 /**
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);
52 /**
53 * Gets some core libraries
55 require_once './libraries/common.inc.php';
56 $blob_streaming_active = $GLOBALS['PMA_Config']->get('BLOBSTREAMING_PLUGINS_EXIST');
58 // Check parameters
59 PMA_checkParameters(array('db', 'table', 'goto'));
61 PMA_DBI_select_db($GLOBALS['db']);
63 /**
64 * Initializes some variables
66 $goto_include = false;
68 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
69 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
70 $GLOBALS['js_include'][] = 'tbl_change.js';
71 require_once './libraries/header.inc.php';
72 require './tbl_change.php';
73 exit;
76 if (isset($_REQUEST['after_insert'])
77 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
78 $url_params['after_insert'] = $_REQUEST['after_insert'];
79 //$GLOBALS['goto'] = 'tbl_change.php';
80 $goto_include = 'tbl_change.php';
82 if (isset($_REQUEST['where_clause'])) {
83 if ($_REQUEST['after_insert'] == 'same_insert') {
84 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
85 $url_params['where_clause'][] = $one_where_clause;
87 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
88 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
89 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
90 . ' WHERE ' . str_replace('` =', '` >', $one_where_clause)
91 . ' LIMIT 1;';
92 $res = PMA_DBI_query($local_query);
93 $row = PMA_DBI_fetch_row($res);
94 $meta = PMA_DBI_get_fields_meta($res);
95 // must find a unique condition based on unique key,
96 // not a combination of all fields
97 list($unique_condition, $clause_is_unique) = PMA_getUniqueCondition($res, count($meta), $meta, $row, true);
98 if (! empty($unique_condition)) {
99 $_SESSION['edit_next'] = $unique_condition;
101 unset($unique_condition, $clause_is_unique);
105 } elseif (! empty($GLOBALS['goto'])) {
106 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
107 // this should NOT happen
108 //$GLOBALS['goto'] = false;
109 $goto_include = false;
110 } else {
111 $goto_include = $GLOBALS['goto'];
113 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
114 $GLOBALS['table'] = '';
118 if (! $goto_include) {
119 if (! strlen($GLOBALS['table'])) {
120 $goto_include = 'db_sql.php';
121 } else {
122 $goto_include = 'tbl_sql.php';
126 // Defines the url to return in case of failure of the query
127 if (isset($_REQUEST['err_url'])) {
128 $err_url = $_REQUEST['err_url'];
129 } else {
130 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
134 * Prepares the update/insert of a row
136 if (isset($_REQUEST['where_clause'])) {
137 // we were editing something => use the WHERE clause
138 $loop_array = (is_array($_REQUEST['where_clause']) ? $_REQUEST['where_clause'] : array($_REQUEST['where_clause']));
139 $using_key = true;
140 $is_insert = ($_REQUEST['submit_type'] == 'insert') || ($_REQUEST['submit_type'] == 'showinsert') || ($_REQUEST['submit_type'] == 'insertignore');
141 $is_insertignore = ($_REQUEST['submit_type'] == 'insertignore');
142 } else {
143 // new row => use indexes
144 $loop_array = array();
145 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
146 $loop_array[] = $key;
148 $using_key = false;
149 $is_insert = true;
150 $is_insertignore = false;
153 $query = array();
154 $value_sets = array();
155 $func_no_param = array(
156 'NOW',
157 'CURDATE',
158 'CURTIME',
159 'UTC_DATE',
160 'UTC_TIME',
161 'UTC_TIMESTAMP',
162 'UNIX_TIMESTAMP',
163 'RAND',
164 'USER',
165 'LAST_INSERT_ID',
166 'UUID',
167 'CURRENT_USER',
170 foreach ($loop_array as $rowcount => $where_clause) {
171 // skip fields to be ignored
172 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
173 continue;
176 // Defines the SET part of the sql query
177 $query_values = array();
179 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
180 $me_fields =
181 isset($_REQUEST['fields']['multi_edit'][$rowcount])
182 ? $_REQUEST['fields']['multi_edit'][$rowcount]
183 : array();
184 $me_fields_name =
185 isset($_REQUEST['fields_name']['multi_edit'][$rowcount])
186 ? $_REQUEST['fields_name']['multi_edit'][$rowcount]
187 : null;
188 $me_fields_prev =
189 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
190 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
191 : null;
192 $me_funcs =
193 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
194 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
195 : null;
196 $me_fields_type =
197 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
198 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
199 : null;
200 $me_fields_null =
201 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
202 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
203 : null;
204 $me_fields_null_prev =
205 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
206 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
207 : null;
208 $me_auto_increment =
209 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
210 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
211 : null;
213 // Fetch the current values of a row to use in case we have a protected field
214 // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
215 if ($is_insert && $using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($where_clause)) {
216 $prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';');
218 foreach ($me_fields as $key => $val) {
220 // Note: $key is an md5 of the fieldname. The actual fieldname is available in $me_fields_name[$key]
222 require './libraries/tbl_replace_fields.inc.php';
224 // for blobstreaming
225 if ($blob_streaming_active) {
226 $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ? $_REQUEST['remove_blob_repo_' . $key] : NULL;
227 $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ? $_REQUEST['upload_blob_repo_' . $key] : NULL;
229 // checks if an existing blob repository reference should be removed
230 if (isset($remove_blob_repo) && ! isset($upload_blob_repo)) {
231 $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
232 if (isset($remove_blob_reference)) {
233 $val = "NULL";
237 // checks if this field requires a bs reference attached to it
238 if (isset($upload_blob_repo)) {
239 // get the most recent BLOB reference
240 $bs_reference = PMA_File::getRecentBLOBReference();
242 // if the most recent BLOB reference exists, set it as a field value
243 if (!is_null($bs_reference)) {
244 $val = "'" . PMA_sqlAddslashes($bs_reference) . "'";
249 if (empty($me_funcs[$key])) {
250 $cur_value = $val;
251 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
252 $cur_value = $me_funcs[$key] . '(' . $val . ')';
253 } elseif ('UUID' === $me_funcs[$key]) {
254 /* This way user will know what UUID new row has */
255 $uuid = PMA_DBI_fetch_value('SELECT UUID()');
256 $cur_value = "'" . $uuid . "'";
257 } elseif (in_array($me_funcs[$key], $func_no_param)) {
258 $cur_value = $me_funcs[$key] . '()';
259 } else {
260 $cur_value = $me_funcs[$key] . '(' . $val . ')';
263 // i n s e r t
264 if ($is_insert) {
265 // no need to add column into the valuelist
266 if (strlen($cur_value)) {
267 $query_values[] = $cur_value;
268 // first inserted row so prepare the list of fields
269 if (empty($value_sets)) {
270 $query_fields[] = PMA_backquote($me_fields_name[$key]);
274 // u p d a t e
275 } elseif (!empty($me_fields_null_prev[$key])
276 && !isset($me_fields_null[$key])) {
277 // field had the null checkbox before the update
278 // field no longer has the null checkbox
279 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
280 } elseif (empty($me_funcs[$key])
281 && isset($me_fields_prev[$key])
282 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
283 // No change for this column and no MySQL function is used -> next column
284 continue;
285 } elseif (! empty($val)) {
286 // avoid setting a field to NULL when it's already NULL
287 // (field had the null checkbox before the update
288 // field still has the null checkbox)
289 if (!(! empty($me_fields_null_prev[$key])
290 && isset($me_fields_null[$key]))) {
291 $query_values[] = PMA_backquote($me_fields_name[$key]) . ' = ' . $cur_value;
294 } // end foreach ($me_fields as $key => $val)
296 if (count($query_values) > 0) {
297 if ($is_insert) {
298 $value_sets[] = implode(', ', $query_values);
299 } else {
300 // build update query
301 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
302 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $where_clause . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1');
306 } // end foreach ($loop_array as $where_clause)
307 unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
308 $me_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, $using_key,
309 $func_no_param);
312 // Builds the sql query
313 if ($is_insert && count($value_sets) > 0) {
314 if ($is_insertignore) {
315 $insert_command = 'INSERT IGNORE ';
316 } else {
317 $insert_command = 'INSERT ';
319 $query[] = $insert_command . 'INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
320 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
321 unset($insert_command);
323 unset($query_fields);
324 } elseif (empty($query)) {
325 // No change -> move back to the calling script
326 $message = PMA_Message::success(__('No change'));
327 $active_page = $goto_include;
328 require_once './libraries/header.inc.php';
329 require './' . PMA_securePath($goto_include);
330 exit;
332 unset($me_fields, $is_insertignore);
335 * Executes the sql query and get the result, then move back to the calling
336 * page
338 if (! empty($GLOBALS['sql_query'])) {
339 $url_params['sql_query'] = $GLOBALS['sql_query'];
340 $return_to_sql_query = $GLOBALS['sql_query'];
342 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
343 // to ensure that the query is displayed in case of
344 // "insert as new row" and then "insert another new row"
345 $GLOBALS['display_query'] = $GLOBALS['sql_query'];
347 $total_affected_rows = 0;
348 $last_messages = array();
349 $warning_messages = array();
350 $error_messages = array();
352 foreach ($query as $single_query) {
353 if ($_REQUEST['submit_type'] == 'showinsert') {
354 $last_messages[] = PMA_Message::notice(__('Showing SQL query'));
355 continue;
357 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
358 $result = PMA_DBI_try_query($single_query);
359 } else {
360 $result = PMA_DBI_query($single_query);
363 if (! $result) {
364 $error_messages[] = PMA_DBI_getError();
365 } else {
366 // the following is a real assignment:
367 if ($tmp = @PMA_DBI_affected_rows()) {
368 $total_affected_rows += $tmp;
370 unset($tmp);
372 $insert_id = PMA_DBI_insert_id();
373 if ($insert_id != 0) {
374 // insert_id is id of FIRST record inserted in one insert, so if we
375 // inserted multiple rows, we had to increment this
377 if ($total_affected_rows > 0) {
378 $insert_id = $insert_id + $total_affected_rows - 1;
380 $last_message = PMA_Message::notice(__('Inserted row id: %1$d'));
381 $last_message->addParam($insert_id);
382 $last_messages[] = $last_message;
384 PMA_DBI_free_result($result);
385 } // end if
387 foreach (PMA_DBI_get_warnings() as $warning) {
388 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
389 . ' ' . $warning['Message'];
392 unset($result);
394 unset($single_query, $query);
396 if ($is_insert && count($value_sets) > 0) {
397 $message = PMA_Message::inserted_rows($total_affected_rows);
398 } else {
399 $message = PMA_Message::affected_rows($total_affected_rows);
402 $message->addMessages($last_messages, '<br />');
404 if (! empty($warning_messages)) {
406 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
407 * the message
409 $message->addMessages($warning_messages, '<br />');
410 $message->isWarning(true);
412 if (! empty($error_messages)) {
413 $message->addMessages($error_messages);
414 $message->isError(true);
416 unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
418 if (isset($return_to_sql_query)) {
419 $disp_query = $GLOBALS['sql_query'];
420 $disp_message = $message;
421 unset($message);
422 $GLOBALS['sql_query'] = $return_to_sql_query;
425 $GLOBALS['js_include'][] = 'tbl_change.js';
426 // in case we call sql.php which needs those:
427 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
429 $active_page = $goto_include;
432 * If user asked for "and then Insert another new row" we have to remove
433 * WHERE clause information so that tbl_change.php does not go back
434 * to the current record
436 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
437 unset($_REQUEST['where_clause']);
441 * Load header.
443 require_once './libraries/header.inc.php';
445 * Load target page.
447 require './' . PMA_securePath($goto_include);
448 exit;