add missing space
[phpmyadmin.git] / tbl_replace.php
blob55e53d9993e61ab00f22baba5d404d27a8cfa079
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 * @todo 'edit_next' tends to not work as expected if used ...
9 * at least there is no order by it needs the original query
10 * and the row number and than replace the LIMIT clause
12 * @package PhpMyAdmin
15 /**
16 * Gets some core libraries
18 require_once 'libraries/common.inc.php';
20 /**
21 * functions implementation for this script
23 require_once 'libraries/insert_edit.lib.php';
25 // Check parameters
26 PMA_Util::checkParameters(array('db', 'table', 'goto'));
28 PMA_DBI_select_db($GLOBALS['db']);
30 /**
31 * Initializes some variables
33 $goto_include = false;
35 $response = PMA_Response::getInstance();
36 $header = $response->getHeader();
37 $scripts = $header->getScripts();
38 $scripts->addFile('makegrid.js');
39 // Needed for generation of Inline Edit anchors
40 $scripts->addFile('sql.js');
41 $scripts->addFile('indexes.js');
42 $scripts->addFile('gis_data_editor.js');
44 // check whether insert row moode, if so include tbl_change.php
45 PMA_isInsertRow();
47 $after_insert_actions = array('new_insert', 'same_insert', 'edit_next');
48 if (isset($_REQUEST['after_insert'])
49 && in_array($_REQUEST['after_insert'], $after_insert_actions)
50 ) {
51 $url_params['after_insert'] = $_REQUEST['after_insert'];
52 if (isset($_REQUEST['where_clause'])) {
53 foreach ($_REQUEST['where_clause'] as $one_where_clause) {
54 if ($_REQUEST['after_insert'] == 'same_insert') {
55 $url_params['where_clause'][] = $one_where_clause;
56 } elseif ($_REQUEST['after_insert'] == 'edit_next') {
57 PMA_setSessionForEditNext($one_where_clause);
62 //get $goto_include for different cases
63 $goto_include = PMA_getGotoInclude($goto_include);
65 // Defines the url to return in case of failure of the query
66 $err_url = PMA_getErrorUrl($url_params);
68 /**
69 * Prepares the update/insert of a row
71 list($loop_array, $using_key, $is_insert, $is_insertignore)
72 = PMA_getParamsForUpdateOrInsert();
74 $query = array();
75 $value_sets = array();
76 $func_no_param = array(
77 'CONNECTION_ID',
78 'CURRENT_USER',
79 'CURDATE',
80 'CURTIME',
81 'CURRENT_DATE',
82 'CURRENT_TIME',
83 'DATABASE',
84 'LAST_INSERT_ID',
85 'NOW',
86 'PI',
87 'RAND',
88 'SYSDATE',
89 'UNIX_TIMESTAMP',
90 'USER',
91 'UTC_DATE',
92 'UTC_TIME',
93 'UTC_TIMESTAMP',
94 'UUID',
95 'UUID_SHORT',
96 'VERSION',
98 $func_optional_param = array(
99 'RAND',
100 'UNIX_TIMESTAMP',
103 $gis_from_text_functions = array(
104 'GeomFromText',
105 'GeomCollFromText',
106 'LineFromText',
107 'MLineFromText',
108 'PointFromText',
109 'MPointFromText',
110 'PolyFromText',
111 'MPolyFromText',
114 $gis_from_wkb_functions = array(
115 'GeomFromWKB',
116 'GeomCollFromWKB',
117 'LineFromWKB',
118 'MLineFromWKB',
119 'PointFromWKB',
120 'MPointFromWKB',
121 'PolyFromWKB',
122 'MPolyFromWKB',
125 // to create an object of PMA_File class
126 require_once './libraries/File.class.php';
128 $query_fields = array();
129 foreach ($loop_array as $rownumber => $where_clause) {
130 // skip fields to be ignored
131 if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
132 continue;
135 // Defines the SET part of the sql query
136 $query_values = array();
138 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
139 $multi_edit_colummns
140 = isset($_REQUEST['fields']['multi_edit'][$rownumber])
141 ? $_REQUEST['fields']['multi_edit'][$rownumber]
142 : array();
143 $multi_edit_columns_name
144 = isset($_REQUEST['fields_name']['multi_edit'][$rownumber])
145 ? $_REQUEST['fields_name']['multi_edit'][$rownumber]
146 : null;
147 $multi_edit_columns_prev
148 = isset($_REQUEST['fields_prev']['multi_edit'][$rownumber])
149 ? $_REQUEST['fields_prev']['multi_edit'][$rownumber]
150 : null;
151 $multi_edit_funcs
152 = isset($_REQUEST['funcs']['multi_edit'][$rownumber])
153 ? $_REQUEST['funcs']['multi_edit'][$rownumber]
154 : null;
155 $multi_edit_columns_type
156 = isset($_REQUEST['fields_type']['multi_edit'][$rownumber])
157 ? $_REQUEST['fields_type']['multi_edit'][$rownumber]
158 : null;
159 $multi_edit_columns_null
160 = isset($_REQUEST['fields_null']['multi_edit'][$rownumber])
161 ? $_REQUEST['fields_null']['multi_edit'][$rownumber]
162 : null;
163 $multi_edit_columns_null_prev
164 = isset($_REQUEST['fields_null_prev']['multi_edit'][$rownumber])
165 ? $_REQUEST['fields_null_prev']['multi_edit'][$rownumber]
166 : null;
167 $multi_edit_auto_increment
168 = isset($_REQUEST['auto_increment']['multi_edit'][$rownumber])
169 ? $_REQUEST['auto_increment']['multi_edit'][$rownumber]
170 : null;
172 // When a select field is nullified, it's not present in $_REQUEST
173 // so initialize it; this way, the foreach($multi_edit_colummns) will process it
174 foreach ($multi_edit_columns_name as $key => $val) {
175 if (! isset($multi_edit_colummns[$key])) {
176 $multi_edit_colummns[$key] = '';
180 // Iterate in the order of $multi_edit_columns_name,
181 // not $multi_edit_colummns, to avoid problems
182 // when inserting multiple entries
183 foreach ($multi_edit_columns_name as $key => $colummn_name) {
184 $current_value = $multi_edit_colummns[$key];
185 // Note: $key is an md5 of the fieldname. The actual fieldname is
186 // available in $multi_edit_columns_name[$key]
188 $file_to_insert = new PMA_File();
189 $file_to_insert->checkTblChangeForm($key, $rownumber);
191 $possibly_uploaded_val = $file_to_insert->getContent();
193 if ($file_to_insert->isError()) {
194 $message .= $file_to_insert->getError();
196 // delete $file_to_insert temporary variable
197 $file_to_insert->cleanUp();
199 $current_value = PMA_getCurrentValueForDifferentTypes(
200 $possibly_uploaded_val, $key, $multi_edit_columns_type,
201 $current_value, $multi_edit_auto_increment,
202 $rownumber, $multi_edit_columns_name, $multi_edit_columns_null,
203 $multi_edit_columns_null_prev, $is_insert,
204 $using_key, $where_clause, $table
207 $current_value_as_an_array = PMA_getCurrentValueAsAnArrayForMultipleEdit(
208 $multi_edit_colummns, $multi_edit_columns_name, $multi_edit_funcs,
209 $gis_from_text_functions, $current_value, $gis_from_wkb_functions,
210 $func_optional_param, $func_no_param, $key
213 list($query_values, $query_fields)
214 = PMA_getQueryValuesForInsertAndUpdateInMultipleEdit(
215 $multi_edit_columns_name, $multi_edit_columns_null, $current_value,
216 $multi_edit_columns_prev, $multi_edit_funcs, $is_insert,
217 $query_values, $query_fields, $current_value_as_an_array,
218 $value_sets, $key, $multi_edit_columns_null_prev
220 } //end of foreach
222 if (count($query_values) > 0) {
223 if ($is_insert) {
224 $value_sets[] = implode(', ', $query_values);
225 } else {
226 // build update query
227 $query[] = 'UPDATE ' . PMA_Util::backquote($GLOBALS['db'])
228 . '.' . PMA_Util::backquote($GLOBALS['table'])
229 . ' SET ' . implode(', ', $query_values)
230 . ' WHERE ' . $where_clause
231 . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1');
234 } // end foreach ($loop_array as $where_clause)
235 unset($multi_edit_columns_name, $multi_edit_columns_prev, $multi_edit_funcs,
236 $multi_edit_columns_type, $multi_edit_columns_null, $func_no_param,
237 $multi_edit_auto_increment, $current_value_as_an_array, $key, $current_value,
238 $loop_array, $where_clause, $using_key, $multi_edit_columns_null_prev);
240 // Builds the sql query
241 if ($is_insert && count($value_sets) > 0) {
242 $query = PMA_buildSqlQuery($is_insertignore, $query_fields, $value_sets);
243 } elseif (empty($query)) {
244 // No change -> move back to the calling script
246 // Note: logic passes here for inline edit
247 $message = PMA_Message::success(__('No change'));
248 $active_page = $goto_include;
249 include '' . PMA_securePath($goto_include);
250 exit;
252 unset($multi_edit_colummns, $is_insertignore);
255 * Executes the sql query and get the result, then move back to the calling
256 * page
258 list ($url_params, $total_affected_rows, $last_messages, $warning_messages,
259 $error_messages, $return_to_sql_query)
260 = PMA_executeSqlQuery($url_params, $query);
262 if ($is_insert && count($value_sets) > 0) {
263 $message = PMA_Message::getMessageForInsertedRows($total_affected_rows);
264 } else {
265 $message = PMA_Message::getMessageForAffectedRows($total_affected_rows);
268 $message->addMessages($last_messages, '<br />');
270 if (! empty($warning_messages)) {
271 $message->addMessages($warning_messages, '<br />');
272 $message->isError(true);
274 if (! empty($error_messages)) {
275 $message->addMessages($error_messages);
276 $message->isError(true);
278 unset(
279 $error_messages, $warning_messages, $total_affected_rows,
280 $last_messages, $last_message
284 * The following section only applies to grid editing.
285 * However, verifying isAjax() is not enough to ensure we are coming from
286 * grid editing. If we are coming from the Edit or Copy link in Browse mode,
287 * ajax_page_request is present in the POST parameters.
289 if ($response->isAjax() && ! isset($_POST['ajax_page_request'])) {
291 * If we are in grid editing, we need to process the relational and
292 * transformed fields, if they were edited. After that, output the correct
293 * link/transformed value and exit
295 * Logic taken from libraries/DisplayResults.class.php
298 if (isset($_REQUEST['rel_fields_list']) && $_REQUEST['rel_fields_list'] != '') {
300 $map = PMA_getForeigners($db, $table, '', 'both');
302 $relation_fields = array();
303 parse_str($_REQUEST['rel_fields_list'], $relation_fields);
305 // loop for each relation cell
306 foreach ($relation_fields as $cell_index => $curr_cell_rel_field) {
307 foreach ($curr_cell_rel_field as $relation_field => $relation_field_value) {
308 $where_comparison = "='" . $relation_field_value . "'";
309 $dispval = PMA_getDisplayValueForForeignTableColumn(
310 $where_comparison, $relation_field_value, $map, $relation_field
313 $extra_data['relations'][$cell_index] = PMA_getLinkForRelationalDisplayField(
314 $map, $relation_field, $where_comparison,
315 $dispval, $relation_field_value
318 } // end of loop for each relation cell
320 if (isset($_REQUEST['do_transformations'])
321 && $_REQUEST['do_transformations'] == true
323 include_once 'libraries/transformations.lib.php';
324 //if some posted fields need to be transformed, generate them here.
325 $mime_map = PMA_getMIME($db, $table);
327 if ($mime_map === false) {
328 $mime_map = array();
330 $edited_values = array();
331 parse_str($_REQUEST['transform_fields_list'], $edited_values);
333 if (! isset($extra_data)) {
334 $extra_data = array();
336 foreach ($mime_map as $transformation) {
337 $file = PMA_securePath($transformation['transformation']);
338 // if only an underscore in the file name, nothing to transform
339 if ($file != '_') {
340 $column_name = $transformation['column_name'];
341 $extra_data = PMA_transformEditedValues(
342 $db, $table, $transformation, $edited_values, $file,
343 $column_name, $extra_data
346 } // end of loop for each $mime_map
349 // Need to check the inline edited value can be truncated by MySQL
350 // without informing while saving
351 $column_name = $_REQUEST['fields_name']['multi_edit'][0][0];
353 PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData(
354 $db, $table, $column_name, $extra_data
357 /**Get the total row count of the table*/
358 $extra_data['row_count'] = PMA_Table::countRecords(
359 $_REQUEST['db'], $_REQUEST['table']
362 $extra_data['sql_query']
363 = PMA_Util::getMessage($message, $GLOBALS['display_query']);
365 $response = PMA_Response::getInstance();
366 $response->isSuccess($message->isSuccess());
367 $response->addJSON('message', $message);
368 $response->addJSON($extra_data);
369 exit;
372 if (! empty($return_to_sql_query)) {
373 $disp_query = $GLOBALS['sql_query'];
374 $disp_message = $message;
375 unset($message);
376 $GLOBALS['sql_query'] = $return_to_sql_query;
379 $scripts->addFile('tbl_change.js');
381 $active_page = $goto_include;
384 * If user asked for "and then Insert another new row" we have to remove
385 * WHERE clause information so that tbl_change.php does not go back
386 * to the current record
388 if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
389 unset($_REQUEST['where_clause']);
393 * Load target page.
395 require '' . PMA_securePath($goto_include);
396 exit;