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
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()
33 * @uses preg_replace()
35 * @uses $GLOBALS['db']
36 * @uses $GLOBALS['table']
37 * @uses $GLOBALS['goto']
38 * @uses $GLOBALS['sql_query']
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';
58 PMA_checkParameters(array('db', 'table', 'goto'));
60 PMA_DBI_select_db($GLOBALS['db']);
63 * Initializes some variables
65 if (isset($_REQUEST['dontlimitchars'])) {
66 $url_params['dontlimitchars'] = $_REQUEST['dontlimitchars'];
68 if (isset($_REQUEST['pos'])) {
69 $url_params['pos'] = (int) $_REQUEST['pos'];
71 if (isset($_REQUEST['session_max_rows'])) {
72 $url_params['session_max_rows'] = (int) $_REQUEST['session_max_rows'];
74 if (isset($_REQUEST['disp_direction'])) {
75 $url_params['disp_direction'] = $_REQUEST['disp_direction'];
77 if (isset($_REQUEST['repeat_cells'])) {
78 $url_params['repeat_cells'] = (int) $_REQUEST['repeat_cells'];
81 $goto_include = false;
82 if (isset($_REQUEST['after_insert'])
83 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
84 $url_params['after_insert'] = $_REQUEST['after_insert'];
85 //$GLOBALS['goto'] = 'tbl_change.php';
86 $goto_include = 'tbl_change.php';
88 if (isset($_REQUEST['primary_key'])) {
89 if ($_REQUEST['after_insert'] == 'same_insert') {
90 foreach ($_REQUEST['primary_key'] as $pk) {
91 $url_params['primary_key'][] = $pk;
93 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
94 foreach ($_REQUEST['primary_key'] as $pk) {
95 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
96 . ' WHERE ' . str_replace('` =', '` >', $pk)
98 $res = PMA_DBI_query($local_query);
99 $row = PMA_DBI_fetch_row($res);
100 $meta = PMA_DBI_get_fields_meta($res);
101 // must find a unique condition based on unique key,
102 // not a combination of all fields
103 if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
104 $_SESSION['edit_next'] = $tmp;
110 } elseif (! empty($GLOBALS['goto'])) {
111 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
112 // this should NOT happen
113 //$GLOBALS['goto'] = false;
114 $goto_include = false;
116 $goto_include = $GLOBALS['goto'];
118 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
119 $GLOBALS['table'] = '';
123 if (! $goto_include) {
124 if (! strlen($GLOBALS['table'])) {
125 $goto_include = 'db_sql.php';
127 $goto_include = 'tbl_sql.php';
131 // Defines the url to return in case of failure of the query
132 if (isset($_REQUEST['err_url'])) {
133 $err_url = $_REQUEST['err_url'];
135 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
139 $seen_binary = false;
142 * Prepares the update/insert of a row
144 if (isset($_REQUEST['primary_key'])) {
145 // we were editing something => use primary key
146 $loop_array = (is_array($_REQUEST['primary_key']) ?
$_REQUEST['primary_key'] : array($_REQUEST['primary_key']));
148 $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
150 // new row => use indexes
151 $loop_array = array();
152 foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
153 $loop_array[] = $key;
161 $value_sets = array();
162 $func_no_param = array(
175 foreach ($loop_array as $primary_key) {
176 // skip fields to be ignored
177 if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) {
181 // Defines the SET part of the sql query
182 $query_values = array();
184 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
186 isset($_REQUEST['fields']['multi_edit'][$primary_key])
187 ?
$_REQUEST['fields']['multi_edit'][$primary_key]
190 isset($_REQUEST['fields_prev']['multi_edit'][$primary_key])
191 ?
$_REQUEST['fields_prev']['multi_edit'][$primary_key]
194 isset($_REQUEST['funcs']['multi_edit'][$primary_key])
195 ?
$_REQUEST['funcs']['multi_edit'][$primary_key]
198 isset($_REQUEST['fields_type']['multi_edit'][$primary_key])
199 ?
$_REQUEST['fields_type']['multi_edit'][$primary_key]
202 isset($_REQUEST['fields_null']['multi_edit'][$primary_key])
203 ?
$_REQUEST['fields_null']['multi_edit'][$primary_key]
205 $me_fields_null_prev =
206 isset($_REQUEST['fields_null_prev']['multi_edit'][$primary_key])
207 ?
$_REQUEST['fields_null_prev']['multi_edit'][$primary_key]
210 isset($_REQUEST['auto_increment']['multi_edit'][$primary_key])
211 ?
$_REQUEST['auto_increment']['multi_edit'][$primary_key]
214 foreach ($me_fields as $key => $val) {
216 require './libraries/tbl_replace_fields.inc.php';
218 if (empty($me_funcs[$key])) {
220 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
221 $cur_value = $me_funcs[$key] . '(' . $val . ')';
222 } elseif (in_array($me_funcs[$key], $func_no_param)) {
223 $cur_value = $me_funcs[$key] . '()';
225 $cur_value = $me_funcs[$key] . '(' . $val . ')';
230 // no need to add column into the valuelist
231 if (strlen($cur_value)) {
232 $query_values[] = $cur_value;
233 $query_fields[] = PMA_backquote($key);
237 } elseif (!empty($me_fields_null_prev[$key])
238 && !isset($me_fields_null[$key])) {
239 // field had the null checkbox before the update
240 // field no longer has the null checkbox
241 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
242 } elseif (empty($me_funcs[$key])
243 && isset($me_fields_prev[$key])
244 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
245 // No change for this column and no MySQL function is used -> next column
247 } elseif (! empty($val)) {
248 // avoid setting a field to NULL when it's already NULL
249 // (field had the null checkbox before the update
250 // field still has the null checkbox)
251 if (!(! empty($me_fields_null_prev[$key])
252 && isset($me_fields_null[$key]))) {
253 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
256 } // end foreach ($me_fields as $key => $val)
258 if (count($query_values) > 0) {
260 $value_sets[] = implode(', ', $query_values);
262 // build update query
263 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
264 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $primary_key . ' LIMIT 1';
268 } // end foreach ($loop_array as $primary_key)
269 unset($me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
270 $me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key,
274 // Builds the sql query
275 if ($is_insert && count($value_sets) > 0) {
276 $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
277 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
279 unset($query_fields, $value_sets);
281 $message .= $GLOBALS['strInsertedRows'] . ' ';
282 } elseif (! empty($query)) {
283 $message .= $GLOBALS['strAffectedRows'] . ' ';
285 // No change -> move back to the calling script
286 $message .= $GLOBALS['strNoModification'];
287 $js_to_run = 'functions.js';
288 $active_page = $goto_include;
289 require_once './libraries/header.inc.php';
290 require './' . PMA_securePath($goto_include);
293 unset($me_fields, $is_insert);
296 * Executes the sql query and get the result, then move back to the calling
299 if (! empty($GLOBALS['sql_query'])) {
300 $url_params['sql_query'] = $GLOBALS['sql_query'];
301 $return_to_sql_query = $GLOBALS['sql_query'];
303 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
304 $total_affected_rows = 0;
306 $warning_message = '';
308 foreach ($query as $single_query) {
309 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
310 $result = PMA_DBI_try_query($single_query);
312 $result = PMA_DBI_query($single_query);
314 if (isset($GLOBALS['warning'])) {
315 $warning_message .= $GLOBALS['warning'] . '[br]';
318 $message .= PMA_DBI_getError();
320 if (@PMA_DBI_affected_rows
()) {
321 $total_affected_rows +
= @PMA_DBI_affected_rows
();
324 $insert_id = PMA_DBI_insert_id();
325 if ($insert_id != 0) {
326 // insert_id is id of FIRST record inserted in one insert, so if we
327 // inserted multiple rows, we had to increment this
329 if ($total_affected_rows > 0) {
330 $insert_id = $insert_id +
$total_affected_rows - 1;
332 $last_message .= '[br]' . $GLOBALS['strInsertedRowId'] . ' ' . $insert_id;
334 PMA_DBI_free_result($result);
338 unset($single_query, $query);
340 $message .= $total_affected_rows . $last_message;
342 if (! empty($warning_message)) {
344 * @todo use a <div class="warning"> in PMA_showMessage() for this part of
347 $message .= '[br]' . $warning_message;
349 unset($warning_message, $total_affected_rows, $last_message);
351 if (isset($return_to_sql_query)) {
352 $disp_query = $GLOBALS['sql_query'];
353 $disp_message = $message;
355 $GLOBALS['sql_query'] = $return_to_sql_query;
358 // if user asked to "Insert another new row", we need tbl_change.js
359 // otherwise the calendar icon does not work
360 if ($goto_include == 'tbl_change.php') {
362 * @todo if we really need to run many different js at header time,
363 * $js_to_run would become an array and header.inc.php would iterate
364 * thru it, instead of the bunch of if/elseif it does now
366 $js_to_run = 'tbl_change.js';
368 $js_to_run = 'functions.js';
370 $active_page = $goto_include;
371 require_once './libraries/header.inc.php';
372 require './' . PMA_securePath($goto_include);