bug #1944077 [core] Config file containing a BOM
[phpmyadmin/dkf.git] / tbl_replace.php
blob3cc0d218d887bb020013def2d70384f094bdb888
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 urlencode()
29 * @uses count()
30 * @uses file_exists()
31 * @uses strlen()
32 * @uses str_replace()
33 * @uses preg_replace()
34 * @uses is_array()
35 * @uses $GLOBALS['db']
36 * @uses $GLOBALS['table']
37 * @uses $GLOBALS['goto']
38 * @uses $GLOBALS['sql_query']
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';
57 // Check parameters
58 PMA_checkParameters(array('db', 'table', 'goto'));
60 PMA_DBI_select_db($GLOBALS['db']);
62 /**
63 * Initializes some variables
65 $goto_include = false;
67 if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
68 $cfg['InsertRows'] = $_REQUEST['insert_rows'];
69 $js_to_run = 'tbl_change.js';
70 require_once './libraries/header.inc.php';
71 require './tbl_change.php';
72 exit;
75 if (isset($_REQUEST['after_insert'])
76 && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
77 $url_params['after_insert'] = $_REQUEST['after_insert'];
78 //$GLOBALS['goto'] = 'tbl_change.php';
79 $goto_include = 'tbl_change.php';
81 if (isset($_REQUEST['primary_key'])) {
82 if ($_REQUEST['after_insert'] == 'same_insert') {
83 foreach ($_REQUEST['primary_key'] as $pk) {
84 $url_params['primary_key'][] = $pk;
86 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
87 foreach ($_REQUEST['primary_key'] as $pk) {
88 $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
89 . ' WHERE ' . str_replace('` =', '` >', $pk)
90 . ' LIMIT 1;';
91 $res = PMA_DBI_query($local_query);
92 $row = PMA_DBI_fetch_row($res);
93 $meta = PMA_DBI_get_fields_meta($res);
94 // must find a unique condition based on unique key,
95 // not a combination of all fields
96 if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
97 $_SESSION['edit_next'] = $tmp;
99 unset($tmp);
103 } elseif (! empty($GLOBALS['goto'])) {
104 if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
105 // this should NOT happen
106 //$GLOBALS['goto'] = false;
107 $goto_include = false;
108 } else {
109 $goto_include = $GLOBALS['goto'];
111 if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
112 $GLOBALS['table'] = '';
116 if (! $goto_include) {
117 if (! strlen($GLOBALS['table'])) {
118 $goto_include = 'db_sql.php';
119 } else {
120 $goto_include = 'tbl_sql.php';
124 // Defines the url to return in case of failure of the query
125 if (isset($_REQUEST['err_url'])) {
126 $err_url = $_REQUEST['err_url'];
127 } else {
128 $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
131 // Misc
132 $seen_binary = false;
135 * Prepares the update/insert of a row
137 if (isset($_REQUEST['primary_key'])) {
138 // we were editing something => use primary key
139 $loop_array = (is_array($_REQUEST['primary_key']) ? $_REQUEST['primary_key'] : array($_REQUEST['primary_key']));
140 $using_key = true;
141 $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
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;
152 $query = array();
153 $message = '';
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',
168 foreach ($loop_array as $rowcount => $primary_key) {
169 // skip fields to be ignored
170 if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) {
171 continue;
174 // Defines the SET part of the sql query
175 $query_values = array();
177 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
178 $me_fields =
179 isset($_REQUEST['fields']['multi_edit'][$rowcount])
180 ? $_REQUEST['fields']['multi_edit'][$rowcount]
181 : array();
182 $me_fields_prev =
183 isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
184 ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
185 : null;
186 $me_funcs =
187 isset($_REQUEST['funcs']['multi_edit'][$rowcount])
188 ? $_REQUEST['funcs']['multi_edit'][$rowcount]
189 : null;
190 $me_fields_type =
191 isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
192 ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
193 : null;
194 $me_fields_null =
195 isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
196 ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
197 : null;
198 $me_fields_null_prev =
199 isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
200 ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
201 : null;
202 $me_auto_increment =
203 isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
204 ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
205 : null;
207 foreach ($me_fields as $key => $val) {
209 require './libraries/tbl_replace_fields.inc.php';
211 if (empty($me_funcs[$key])) {
212 $cur_value = $val;
213 } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
214 $cur_value = $me_funcs[$key] . '(' . $val . ')';
215 } elseif (in_array($me_funcs[$key], $func_no_param)) {
216 $cur_value = $me_funcs[$key] . '()';
217 } else {
218 $cur_value = $me_funcs[$key] . '(' . $val . ')';
221 // i n s e r t
222 if ($is_insert) {
223 // no need to add column into the valuelist
224 if (strlen($cur_value)) {
225 $query_values[] = $cur_value;
226 // first inserted row so prepare the list of fields
227 if (empty($value_sets)) {
228 $query_fields[] = PMA_backquote($key);
232 // u p d a t e
233 } elseif (!empty($me_fields_null_prev[$key])
234 && !isset($me_fields_null[$key])) {
235 // field had the null checkbox before the update
236 // field no longer has the null checkbox
237 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
238 } elseif (empty($me_funcs[$key])
239 && isset($me_fields_prev[$key])
240 && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
241 // No change for this column and no MySQL function is used -> next column
242 continue;
243 } elseif (! empty($val)) {
244 // avoid setting a field to NULL when it's already NULL
245 // (field had the null checkbox before the update
246 // field still has the null checkbox)
247 if (!(! empty($me_fields_null_prev[$key])
248 && isset($me_fields_null[$key]))) {
249 $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
252 } // end foreach ($me_fields as $key => $val)
254 if (count($query_values) > 0) {
255 if ($is_insert) {
256 $value_sets[] = implode(', ', $query_values);
257 } else {
258 // build update query
259 $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
260 . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $primary_key . ' LIMIT 1';
264 } // end foreach ($loop_array as $primary_key)
265 unset($me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
266 $me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key,
267 $func_no_param);
270 // Builds the sql query
271 if ($is_insert && count($value_sets) > 0) {
272 $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
273 . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
275 unset($query_fields, $value_sets);
277 $message .= $GLOBALS['strInsertedRows'] . '&nbsp;';
278 } elseif (! empty($query)) {
279 $message .= $GLOBALS['strAffectedRows'] . '&nbsp;';
280 } else {
281 // No change -> move back to the calling script
282 $message .= $GLOBALS['strNoModification'];
283 $js_to_run = 'functions.js';
284 $active_page = $goto_include;
285 require_once './libraries/header.inc.php';
286 require './' . PMA_securePath($goto_include);
287 exit;
289 unset($me_fields, $is_insert);
292 * Executes the sql query and get the result, then move back to the calling
293 * page
295 if (! empty($GLOBALS['sql_query'])) {
296 $url_params['sql_query'] = $GLOBALS['sql_query'];
297 $return_to_sql_query = $GLOBALS['sql_query'];
299 $GLOBALS['sql_query'] = implode('; ', $query) . ';';
300 $total_affected_rows = 0;
301 $last_message = '';
302 $warning_message = '';
304 foreach ($query as $single_query) {
305 if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
306 $result = PMA_DBI_try_query($single_query);
307 } else {
308 $result = PMA_DBI_query($single_query);
311 if (! $result) {
312 $message .= PMA_DBI_getError();
313 } else {
314 if (@PMA_DBI_affected_rows()) {
315 $total_affected_rows += @PMA_DBI_affected_rows();
318 $insert_id = PMA_DBI_insert_id();
319 if ($insert_id != 0) {
320 // insert_id is id of FIRST record inserted in one insert, so if we
321 // inserted multiple rows, we had to increment this
323 if ($total_affected_rows > 0) {
324 $insert_id = $insert_id + $total_affected_rows - 1;
326 $last_message .= '[br]' . $GLOBALS['strInsertedRowId'] . '&nbsp;' . $insert_id;
328 PMA_DBI_free_result($result);
329 } // end if
331 foreach (PMA_DBI_get_warnings() as $warning) {
332 $warning_message .= $warning['Level'] . ': #' . $warning['Code']
333 . ' ' . $warning['Message'] . '[br]';
336 unset($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
345 * the message
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;
354 unset($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';
367 } else {
368 $js_to_run = 'functions.js';
370 $active_page = $goto_include;
371 require_once './libraries/header.inc.php';
372 require './' . PMA_securePath($goto_include);
373 exit;