3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Gets some core libraries
8 require_once('./libraries/common.lib.php');
11 PMA_checkParameters(array('db', 'table', 'goto'));
13 PMA_DBI_select_db($db);
16 * Initializes some variables
18 // Defines the url to return in case of success of the query
19 if (isset($sql_query)) {
20 $sql_query = urldecode($sql_query);
22 if (!isset($dontlimitchars)) {
29 if (isset($after_insert) && $after_insert == 'new_insert') {
30 $goto = 'tbl_change.php?'
31 . PMA_generate_common_url($db, $table, '&')
32 . '&goto=' . urlencode($goto)
34 . '&session_max_rows=' . $session_max_rows
35 . '&disp_direction=' . $disp_direction
36 . '&repeat_cells=' . $repeat_cells
37 . '&dontlimitchars=' . $dontlimitchars
38 . '&after_insert=' . $after_insert
39 . (empty($sql_query) ?
'' : '&sql_query=' . urlencode($sql_query));
40 } elseif (isset($after_insert) && $after_insert == 'same_insert') {
41 $goto = 'tbl_change.php?'
42 . PMA_generate_common_url($db, $table, '&')
43 . '&goto=' . urlencode($goto)
45 . '&session_max_rows=' . $session_max_rows
46 . '&disp_direction=' . $disp_direction
47 . '&repeat_cells=' . $repeat_cells
48 . '&dontlimitchars=' . $dontlimitchars
49 . '&after_insert=' . $after_insert
50 . (empty($sql_query) ?
'' : '&sql_query=' . urlencode($sql_query));
51 if (isset($primary_key)) {
52 foreach ($primary_key AS $pk) {
53 $goto .= '&primary_key[]=' . $pk;
56 } elseif (isset($after_insert) && $after_insert == 'edit_next') {
57 $goto = 'tbl_change.php?'
58 . PMA_generate_common_url($db, $table, '&')
59 . '&goto=' . urlencode($goto)
61 . '&session_max_rows=' . $session_max_rows
62 . '&disp_direction=' . $disp_direction
63 . '&repeat_cells=' . $repeat_cells
64 . '&dontlimitchars=' . $dontlimitchars
65 . '&after_insert=' . $after_insert
66 . (empty($sql_query) ?
'' : '&sql_query=' . urlencode($sql_query));
67 if (isset($primary_key)) {
68 foreach ($primary_key AS $pk) {
69 $local_query = 'SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . str_replace('` =', '` >', urldecode($pk)) . ' LIMIT 1;';
70 $res = PMA_DBI_query($local_query);
71 $row = PMA_DBI_fetch_row($res);
72 $meta = PMA_DBI_get_fields_meta($res);
73 $goto .= '&primary_key[]=' . urlencode(PMA_getUvaCondition($res, count($row), $meta, $row));
76 } elseif ($goto == 'sql.php') {
78 . PMA_generate_common_url($db, $table, '&')
80 . '&session_max_rows=' . $session_max_rows
81 . '&disp_direction=' . $disp_direction
82 . '&repeat_cells=' . $repeat_cells
83 . '&dontlimitchars=' . $dontlimitchars
84 . '&sql_query=' . urlencode($sql_query);
85 } elseif (!empty($goto)) {
87 $is_gotofile = preg_replace('@^([^?]+).*$@', '\\1', $goto);
88 if (!@file_exists
('./' . $is_gotofile)) {
89 $goto = (! isset($table) ||
! strlen($table)) ?
'db_details.php' : 'tbl_properties.php';
92 $is_gotofile = ($is_gotofile == $goto);
96 // Defines the url to return in case of failure of the query
97 if (isset($err_url)) {
98 $err_url = urldecode($err_url);
100 $err_url = str_replace('&', '&', $goto)
101 . (empty($primary_key) ?
'' : '&primary_key=' . (is_array($primary_key) ?
$primary_key[0] : $primary_key));
105 $seen_binary = FALSE;
108 * Prepares the update/insert of a row
110 if (isset($primary_key)) {
111 // we were editing something => use primary key
112 $loop_array = (is_array($primary_key) ?
$primary_key : array(0 => $primary_key));
114 $is_insert = ($submit_type == $strInsertAsNewRow);
116 // new row => use indexes
117 $loop_array = array();
118 for ($i = 0; $i < $cfg['InsertRows']; $i++
) $loop_array[$i] = $i;
126 foreach ($loop_array AS $primary_key_index => $enc_primary_key) {
127 // skip fields to be ignored
128 if (!$using_key && isset($GLOBALS['insert_ignore_' . $enc_primary_key])) {
132 // Restore the "primary key" to a convenient format
133 $primary_key = urldecode($enc_primary_key);
135 // Defines the SET part of the sql query
139 // Map multi-edit keys to single-level arrays, dependent on how we got the fields
140 $me_fields = isset($fields['multi_edit']) && isset($fields['multi_edit'][$enc_primary_key]) ?
$fields['multi_edit'][$enc_primary_key] : null;
141 $me_fields_prev = isset($fields_prev['multi_edit']) && isset($fields_prev['multi_edit'][$enc_primary_key]) ?
$fields_prev['multi_edit'][$enc_primary_key] : null;
142 $me_funcs = isset($funcs['multi_edit']) && isset($funcs['multi_edit'][$enc_primary_key]) ?
$funcs['multi_edit'][$enc_primary_key] : null;
143 $me_fields_type = isset($fields_type['multi_edit']) && isset($fields_type['multi_edit'][$enc_primary_key]) ?
$fields_type['multi_edit'][$enc_primary_key] : null;
144 $me_fields_null = isset($fields_null['multi_edit']) && isset($fields_null['multi_edit'][$enc_primary_key]) ?
$fields_null['multi_edit'][$enc_primary_key] : null;
145 $me_fields_null_prev = isset($fields_null_prev['multi_edit']) && isset($fields_null_prev['multi_edit'][$enc_primary_key]) ?
$fields_null_prev['multi_edit'][$enc_primary_key] : null;
146 $me_auto_increment = isset($auto_increment['multi_edit']) && isset($auto_increment['multi_edit'][$enc_primary_key]) ?
$auto_increment['multi_edit'][$enc_primary_key] : null;
148 if ($using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($primary_key)) {
149 $prot_result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';');
150 $prot_row = PMA_DBI_fetch_assoc($prot_result);
151 PMA_DBI_free_result($prot_result);
155 foreach ($me_fields AS $encoded_key => $val) {
156 $key = urldecode($encoded_key);
157 $fieldlist .= PMA_backquote($key) . ', ';
159 require('./libraries/tbl_replace_fields.inc.php');
161 if (empty($me_funcs[$encoded_key])) {
162 $cur_value = $val . ', ';
163 } elseif (preg_match('@^(UNIX_TIMESTAMP)$@', $me_funcs[$encoded_key]) && $val != '\'\'') {
164 $cur_value = $me_funcs[$encoded_key] . '(' . $val . '), ';
165 } elseif (preg_match('@^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$@', $me_funcs[$encoded_key])) {
166 $cur_value = $me_funcs[$encoded_key] . '(), ';
168 $cur_value = $me_funcs[$encoded_key] . '(' . $val . '), ';
172 // insert, no need to add column
173 $valuelist .= $cur_value;
174 } elseif (isset($me_fields_null_prev) && isset($me_fields_null_prev[$encoded_key]) && !isset($me_fields_null[$encoded_key])) {
175 // field had the null checkbox
176 // field no longer has the null checkbox
177 // field does not have the same value
178 $valuelist .= PMA_backquote($key) . ' = ' . $cur_value;
179 } elseif (empty($me_funcs[$encoded_key])
180 && isset($me_fields_prev) && isset($me_fields_prev[$encoded_key])
181 && ("'" . PMA_sqlAddslashes(urldecode($me_fields_prev[$encoded_key])) . "'" == $val)) {
182 // No change for this column and no MySQL function is used -> next column
184 } elseif (!empty($val)) {
185 // TODO: avoid setting a field to NULL when it's already NULL
186 $valuelist .= PMA_backquote($key) . ' = ' . $cur_value;
191 $valuelist = preg_replace('@, $@', '', $valuelist);
193 // Builds the sql query
196 // first inserted row -> prepare template
197 $fieldlist = preg_replace('@, $@', '', $fieldlist);
198 $query = array('INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES ');
200 // append current values
201 $query[0] .= '(' . $valuelist . '), ';
202 $message = $strInsertedRows . ' ';
203 } elseif (!empty($valuelist)) {
204 // build update query
205 $query[] = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key . ' LIMIT 1';
207 $message = $strAffectedRows . ' ';
211 // trim last , from insert query
213 $query[0] = preg_replace('@, $@', '', $query[0]);
216 if (empty($valuelist) && empty($query)) {
217 // No change -> move back to the calling script
218 $message = $strNoModification;
220 $js_to_run = 'functions.js';
221 require_once('./libraries/header.inc.php');
222 require('./' . PMA_securePath($goto));
224 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . '&disp_query=');
231 * Executes the sql query and get the result, then move back to the calling
234 $sql_query = implode(';', $query) . ';';
235 $total_affected_rows = 0;
237 $warning_message = '';
239 foreach ($query AS $query_index => $single_query) {
240 if ($cfg['IgnoreMultiSubmitErrors']) {
241 $result = PMA_DBI_try_query($single_query);
243 $result = PMA_DBI_query($single_query);
245 if (isset($GLOBALS['warning'])) {
246 $warning_message .= $GLOBALS['warning'] . '[br]';
249 $message .= PMA_DBI_getError();
251 if (@PMA_DBI_affected_rows
()) {
252 $total_affected_rows +
= @PMA_DBI_affected_rows
();
255 $insert_id = PMA_DBI_insert_id();
256 if ($insert_id != 0) {
257 $last_message .= '[br]'.$strInsertedRowId . ' ' . $insert_id;
260 PMA_DBI_free_result($result);
264 if ($total_affected_rows != 0) {
265 $message .= $total_affected_rows;
267 $message .= $strModifications;
270 $message .= $last_message;
272 if (!empty($warning_message)) {
273 // TODO: use a <div class="warning"> in PMA_showMessage()
274 // for this part of the message
275 $message .= '[br]' . $warning_message;
279 if ($goto == 'db_details.php' && isset($table)) {
282 $js_to_run = 'functions.js';
283 $active_page = $goto;
284 require_once('./libraries/header.inc.php');
285 require('./' . PMA_securePath($goto));
288 // if we have seen binary,
289 // we do not append the query to the Location so it won't be displayed
290 // on the resulting page
291 // Nijel: we also need to limit size of url...
292 $add_query = (!$seen_binary && strlen($sql_query) < 1024 ?
'&disp_query=' . urlencode($sql_query) : '');
293 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . $goto . '&disp_message=' . urlencode($message) . $add_query);