bug #546981
[phpmyadmin/arisferyanto.git] / tbl_change.php3
blob7df7242f47fb727d3aed12deb0d9f99fe6b811a5
1 <?php
2 /* $Id$ */
5 /**
6 * Get the variables sent or posted to this script and displays the header
7 */
8 require('./libraries/grab_globals.lib.php3');
9 $js_to_run = 'tbl_change.js';
10 include('./header.inc.php3');
11 // Displays the query submitted and its result
12 if (!empty($message)) {
13 if (isset($goto)) {
14 $goto_cpy = $goto;
15 $goto = 'tbl_properties.php3'
16 . '?lang=' . $lang
17 . '&amp;server=' . $server
18 . '&amp;db=' . urlencode($db)
19 . '&amp;table=' . urlencode($table)
20 . '&amp;$show_query=y'
21 . '&amp;sql_query=' . urlencode($disp_query);
22 } else {
23 $show_query = 'y';
25 if (isset($sql_query)) {
26 $sql_query_cpy = $sql_query;
27 unset($sql_query);
29 if (isset($disp_query)) {
30 $sql_query = (get_magic_quotes_gpc() ? stripslashes($disp_query) : $disp_query);
32 PMA_showMessage($message);
33 if (isset($goto_cpy)) {
34 $goto = $goto_cpy;
35 unset($goto_cpy);
37 if (isset($sql_query_cpy)) {
38 $sql_query = $sql_query_cpy;
39 unset($sql_query_cpy);
42 if (get_magic_quotes_gpc()) {
43 if (!empty($sql_query)) {
44 $sql_query = stripslashes($sql_query);
46 if (!empty($primary_key)) {
47 $primary_key = stripslashes($primary_key);
49 } // end if
52 /**
53 * Defines the url to return to in case of error in a sql statement
55 if (!isset($goto)) {
56 $goto = 'db_details.php3';
58 if ($goto != 'db_details.php3' && $goto != 'tbl_properties.php3') {
59 $err_url = $goto;
60 } else {
61 $err_url = $goto
62 . '?lang=' . $lang
63 . '&amp;server=' . $server
64 . '&amp;db=' . urlencode($db)
65 . (($goto == 'tbl_properties.php3') ? '&amp;table=' . urlencode($table) : '');
69 /**
70 * Get the list of the fields of the current table
72 mysql_select_db($db);
73 $table_def = mysql_query('SHOW FIELDS FROM ' . PMA_backquote($table));
74 if (isset($primary_key)) {
75 $local_query = 'SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $primary_key;
76 $result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
77 $row = mysql_fetch_array($result);
78 // No row returned
79 if (!$row) {
80 unset($row);
81 unset($primary_key);
82 $goto_cpy = $goto;
83 $goto = 'tbl_properties.php3'
84 . '?lang=' . $lang
85 . '&amp;server=' . $server
86 . '&amp;db=' . urlencode($db)
87 . '&amp;table=' . urlencode($table)
88 . '&amp;$show_query=y'
89 . '&amp;sql_query=' . urlencode($local_query);
90 if (isset($sql_query)) {
91 $sql_query_cpy = $sql_query;
92 unset($sql_query);
94 $sql_query = $local_query;
95 PMA_showMessage($strEmptyResultSet);
96 $goto = $goto_cpy;
97 unset($goto_cpy);
98 if (isset($sql_query_cpy)) {
99 $sql_query = $sql_query_cpy;
100 unset($sql_query_cpy);
102 } // end if (no record returned)
104 else
106 $local_query = 'SELECT * FROM ' . PMA_backquote($table) . ' LIMIT 1';
107 $result = mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
108 unset($row);
113 * Displays the form
115 // loic1: autocomplete feature of IE kills the "onchange" event handler and it
116 // must be replaced by the "onpropertychange" one in this case
117 $chg_evt_handler = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER >= 5)
118 ? 'onpropertychange'
119 : 'onchange';
120 // Had to put the URI because when hosted on an https server,
121 // some browsers send wrongly this form to the http server.
124 <!-- Change table properties form -->
125 <form method="post" action="tbl_replace.php3" name="insertForm">
126 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
127 <input type="hidden" name="server" value="<?php echo $server; ?>" />
128 <input type="hidden" name="db" value="<?php echo $db; ?>" />
129 <input type="hidden" name="table" value="<?php echo $table; ?>" />
130 <input type="hidden" name="goto" value="<?php echo $goto; ?>" />
131 <input type="hidden" name="pos" value="<?php echo isset($pos) ? $pos : 0; ?>" />
132 <input type="hidden" name="session_max_rows" value="<?php echo isset($session_max_rows) ? $session_max_rows : ''; ?>" />
133 <input type="hidden" name="disp_direction" value="<?php echo isset($disp_direction) ? $disp_direction : ''; ?>" />
134 <input type="hidden" name="repeat_cells" value="<?php echo isset($repeat_cells) ? $repeat_cells : ''; ?>" />
135 <input type="hidden" name="err_url" value="<?php echo urlencode($err_url); ?>" />
136 <input type="hidden" name="sql_query" value="<?php echo isset($sql_query) ? urlencode($sql_query) : ''; ?>" />
137 <?php
138 if (isset($primary_key)) {
140 <input type="hidden" name="primary_key" value="<?php echo urlencode($primary_key); ?>" />
141 <?php
143 echo "\n";
146 <table border="<?php echo $cfgBorder; ?>">
147 <tr>
148 <th><?php echo $strField; ?></th>
149 <th><?php echo $strType; ?></th>
150 <?php
151 if ($cfgShowFunctionFields) {
152 echo ' <th>' . $strFunction . '</th>' . "\n";
155 <th><?php echo $strNull; ?></th>
156 <th><?php echo $strValue; ?></th>
157 </tr>
159 <?php
160 // Set if we passed the first timestamp field
161 $timestamp_seen = 0;
162 $fields_cnt = mysql_num_rows($table_def);
164 for ($i = 0; $i < $fields_cnt; $i++) {
165 $row_table_def = mysql_fetch_array($table_def);
166 $field = $row_table_def['Field'];
167 if ($row_table_def['Type'] == 'datetime' && empty($row[$field])) {
168 $row[$field] = date('Y-m-d H:i:s', time());
170 $len = (eregi('float|double', $row_table_def['Type']))
171 ? 100
172 : @mysql_field_len($result, $i);
173 $first_timestamp = 0;
175 $bgcolor = ($i % 2) ? $cfgBgcolorOne : $cfgBgcolorTwo;
177 <tr>
178 <td align="center" bgcolor="<?php echo $bgcolor; ?>"><?php echo htmlspecialchars($field); ?></td>
179 <?php
180 echo "\n";
182 // The type column
183 $is_binary = eregi(' binary', $row_table_def['Type']);
184 $is_blob = eregi('blob', $row_table_def['Type']);
185 $row_table_def['True_Type'] = ereg_replace('\\(.*', '', $row_table_def['Type']);
186 switch ($row_table_def['True_Type']) {
187 case 'set':
188 $type = 'set';
189 $type_nowrap = '';
190 break;
191 case 'enum':
192 $type = 'enum';
193 $type_nowrap = '';
194 break;
195 case 'timestamp':
196 if (!$timestamp_seen) { // can only occur once per table
197 $timestamp_seen = 1;
198 $first_timestamp = 1;
200 $type = $row_table_def['Type'];
201 break;
202 default:
203 $type = $row_table_def['Type'];
204 $type_nowrap = ' nowrap="nowrap"';
205 break;
208 <td align="center" bgcolor="<?php echo $bgcolor; ?>"<?php echo $type_nowrap; ?>><?php echo $type; ?></td>
209 <?php
210 echo "\n";
212 // Prepares the field value
213 if (isset($row)) {
214 // loic1: null field value
215 if (!isset($row[$field])) {
216 $row[$field] = 'NULL';
217 $special_chars = '';
218 $data = $row[$field];
219 } else {
220 // loic1: special binary "characters"
221 if ($is_binary || $is_blob) {
222 $row[$field] = str_replace("\x00", '\0', $row[$field]);
223 $row[$field] = str_replace("\x08", '\b', $row[$field]);
224 $row[$field] = str_replace("\x0a", '\n', $row[$field]);
225 $row[$field] = str_replace("\x0d", '\r', $row[$field]);
226 $row[$field] = str_replace("\x1a", '\Z', $row[$field]);
227 } // end if
228 $special_chars = htmlspecialchars($row[$field]);
229 $data = $row[$field];
230 } // end if... else...
231 // loic1: if a timestamp field value is not included in an update
232 // statement MySQL auto-update it to the current timestamp
233 $backup_field = ($row_table_def['True_Type'] == 'timestamp')
234 ? ''
235 : '<input type="hidden" name="fields_prev[' . urlencode($field) . ']" value="' . urlencode($row[$field]) . '" />';
236 } else {
237 // loic1: display default values
238 if (!isset($row_table_def['Default'])) {
239 $row_table_def['Default'] = '';
240 $data = 'NULL';
241 } else {
242 $data = $row_table_def['Default'];
244 $special_chars = htmlspecialchars($row_table_def['Default']);
245 $backup_field = '';
248 // The function column
249 // -------------------
250 // Change by Bernard M. Piller <bernard@bmpsystems.com>
251 // We don't want binary data to be destroyed
252 // Note: from the MySQL manual: "BINARY doesn't affect how the column is
253 // stored or retrieved" so it does not mean that the contents is
254 // binary
255 if ($cfgShowFunctionFields) {
256 if (($cfgProtectBinary && $is_blob)
257 || ($cfgProtectBinary == 'all' && $is_binary)) {
258 echo ' <td align="center" bgcolor="'. $bgcolor . '">' . $strBinary . '</td>' . "\n";
259 } else if (strstr($row_table_def['True_Type'], 'enum') || strstr($row_table_def['True_Type'], 'set')) {
260 echo ' <td align="center" bgcolor="'. $bgcolor . '">--</td>' . "\n";
261 } else {
263 <td bgcolor="<?php echo $bgcolor; ?>">
264 <select name="funcs[<?php echo urlencode($field); ?>]" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo ($i+2*$fields_cnt); ?>" >
265 <option></option>
266 <?php
267 echo "\n";
268 $selected = '';
269 for ($j = 0; $j < count($cfgFunctions); $j++) {
270 // for default function = NOW() on first timestamp field
271 // -- swix/18jul01
272 $selected = ($first_timestamp && $cfgFunctions[$j] == 'NOW')
273 ? ' selected="selected"'
274 : '';
275 echo ' ';
276 echo '<option' . $selected . '>' . $cfgFunctions[$j] . '</option>' . "\n";
277 } // end for
278 unset($selected);
280 </select>
281 </td>
282 <?php
284 } // end if ($cfgShowFunctionFields)
285 echo "\n";
287 // The null column
288 // ---------------
289 echo ' <td bgcolor="' . $bgcolor . '">' . "\n";
290 if (!(($cfgProtectBinary && $is_blob) || ($cfgProtectBinary == 'all' && $is_binary))
291 && $row_table_def['Null'] == 'YES') {
292 echo ' <input type="checkbox" tabindex=' . ($i+3*$fields_cnt) . '"'
293 . ' name="fields_null[' . urlencode($field) . ']"';
294 if ($data == 'NULL' && !$first_timestamp) {
295 echo ' checked="checked"';
297 $onclick = ' onclick="if (this.checked) {nullify(';
298 if (strstr($row_table_def['True_Type'], 'enum')) {
299 if (strlen($row_table_def['Type']) > 20) {
300 $onclick .= '1, ';
301 } else {
302 $onclick .= '2, ';
304 } else if (strstr($row_table_def['True_Type'], 'set')) {
305 $onclick .= '3, ';
306 } else {
307 $onclick .= '4, ';
309 $onclick .= '\'' . urlencode($field) . '\', \'' . md5($field) . '\'); this.checked = true}; return true" />' . "\n";
310 echo $onclick;
311 } else {
312 echo ' &nbsp;' . "\n";
314 echo ' </td>' . "\n";
316 // The value column (depends on type)
317 // ----------------
318 if (strstr($row_table_def['True_Type'], 'text')) {
320 <td bgcolor="<?php echo $bgcolor; ?>">
321 <?php echo $backup_field . "\n"; ?>
322 <textarea name="fields[<?php echo urlencode($field); ?>]" rows="<?php echo $cfgTextareaRows; ?>" cols="<?php echo $cfgTextareaCols; ?>" wrap="virtual" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo $i; ?>"><?php echo $special_chars; ?></textarea>
323 </td>
324 <?php
325 echo "\n";
326 if (strlen($special_chars) > 32000) {
327 echo ' <td bgcolor="' . $bgcolor . '">' . $strTextAreaLength . '</td>' . "\n";
330 else if (strstr($row_table_def['True_Type'], 'enum')) {
331 $enum = str_replace('enum(', '', $row_table_def['Type']);
332 $enum = ereg_replace('\\)$', '', $enum);
333 $enum = explode('\',\'', substr($enum, 1, -1));
334 $enum_cnt = count($enum);
336 <td bgcolor="<?php echo $bgcolor; ?>">
337 <input type="hidden" name="fields[<?php echo urlencode($field); ?>]" value="$enum$" tabindex="<?php echo $i+1; ?>" />
338 <?php
339 echo "\n" . ' ' . $backup_field;
341 // show dropdown or radio depend on length
342 if (strlen($row_table_def['Type']) > 20) {
343 echo "\n";
345 <select name="field_<?php echo md5($field); ?>[]" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo $i+1; ?>">
346 <option value=""></option>
347 <?php
348 echo "\n";
350 for ($j = 0; $j < $enum_cnt; $j++) {
351 // Removes automatic MySQL escape format
352 $enum_atom = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $enum[$j]));
353 echo ' ';
354 echo '<option value="' . urlencode($enum_atom) . '"';
355 if ($data == $enum_atom
356 || ($data == '' && (!isset($primary_key) || $row_table_def['Null'] != 'YES')
357 && isset($row_table_def['Default']) && $enum_atom == $row_table_def['Default'])) {
358 echo ' selected="selected"';
360 echo '>' . htmlspecialchars($enum_atom) . '</option>' . "\n";
361 } // end for
364 </select>
365 <?php
366 } // end if
367 else {
368 echo "\n";
369 for ($j = 0; $j < $enum_cnt; $j++) {
370 // Removes automatic MySQL escape format
371 $enum_atom = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $enum[$j]));
372 echo ' ';
373 echo '<input type="radio" name="field_' . md5($field) . '[]" value="' . urlencode($enum_atom) . '" id="radio_field_' . md5($field) . '_' . urlencode($enum_atom) . '" onclick="if (typeof(document.forms[\'insertForm\'].elements[\'fields_null[' . urlencode($field) . ']\']) != \'undefined\') {document.forms[\'insertForm\'].elements[\'fields_null[' . urlencode($field) .']\'].checked = false}"';
374 if ($data == $enum_atom
375 || ($data == '' && (!isset($primary_key) || $row_table_def['Null'] != 'YES')
376 && isset($row_table_def['Default']) && $enum_atom == $row_table_def['Default'])) {
377 echo ' checked="checked"';
379 echo 'tabindex="' . $i . '" />' . "\n";
380 echo ' <label for="radio_field_' . md5($field) . '_' . urlencode($enum_atom) . '">' . htmlspecialchars($enum_atom) . '</label>' . "\n";
381 } // end for
383 } // end else
384 echo "\n";
386 </td>
387 <?php
388 echo "\n";
390 else if (strstr($row_table_def['Type'], 'set')) {
391 $set = str_replace('set(', '', $row_table_def['Type']);
392 $set = ereg_replace('\)$', '', $set);
393 $set = explode(',', $set);
395 if (isset($vset)) {
396 unset($vset);
398 for ($vals = explode(',', $data); list($t, $k) = each($vals);) {
399 $vset[$k] = 1;
401 $size = min(4, count($set));
403 <td bgcolor="<?php echo $bgcolor; ?>">
404 <?php echo $backup_field . "\n"; ?>
405 <input type="hidden" name="fields[<?php echo urlencode($field); ?>]" value="$set$" />
406 <select name="field_<?php echo md5($field); ?>[]" size="<?php echo $size; ?>" multiple="multiple" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo $i+1; ?>" >
407 <?php
408 echo "\n";
409 $countset = count($set);
410 for ($j = 0; $j < $countset;$j++) {
411 $subset = substr($set[$j], 1, -1);
412 // Removes automatic MySQL escape format
413 $subset = str_replace('\'\'', '\'', str_replace('\\\\', '\\', $subset));
414 echo ' ';
415 echo '<option value="'. urlencode($subset) . '"';
416 if (isset($vset[$subset]) && $vset[$subset]) {
417 echo ' selected="selected"';
419 echo '>' . htmlspecialchars($subset) . '</option>' . "\n";
420 } // end for
422 </select>
423 </td>
424 <?php
426 // Change by Bernard M. Piller <bernard@bmpsystems.com>
427 // We don't want binary data destroyed
428 else if ($is_binary || $is_blob) {
429 if (($cfgProtectBinary && $is_blob)
430 || ($cfgProtectBinary == 'all' && $is_binary)) {
431 echo "\n";
433 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
434 <?php echo $strBinaryDoNotEdit . "\n"; ?>
435 </td>
436 <?php
437 } else if ($is_blob) {
438 echo "\n";
440 <td bgcolor="<?php echo $bgcolor; ?>">
441 <?php echo $backup_field . "\n"; ?>
442 <textarea name="fields[<?php echo urlencode($field); ?>]" rows="<?php echo $cfgTextareaRows; ?>" cols="<?php echo $cfgTextareaCols; ?>" wrap="virtual" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo $i+1; ?>" ><?php echo $special_chars; ?></textarea>
443 </td>
444 <?php
445 } else {
446 if ($len < 4) {
447 $fieldsize = $maxlength = 4;
448 } else {
449 $fieldsize = (($len > 40) ? 40 : $len);
450 $maxlength = $len;
452 echo "\n";
454 <td bgcolor="<?php echo $bgcolor; ?>">
455 <?php echo $backup_field . "\n"; ?>
456 <input type="text" name="fields[<?php echo urlencode($field); ?>]" value="<?php echo $special_chars; ?>" size="<?php echo $fieldsize; ?>" maxlength="<?php echo $maxlength; ?>" class="textfield" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo $i+1; ?>" />
457 </td>
458 <?php
459 } // end if...elseif...else
460 } // end else if
461 else {
462 if ($len < 4) {
463 $fieldsize = $maxlength = 4;
464 } else {
465 $fieldsize = (($len > 40) ? 40 : $len);
466 $maxlength = $len;
468 echo "\n";
470 <td bgcolor="<?php echo $bgcolor; ?>">
471 <?php echo $backup_field . "\n"; ?>
472 <input type="text" name="fields[<?php echo urlencode($field); ?>]" value="<?php echo $special_chars; ?>" size="<?php echo $fieldsize; ?>" maxlength="<?php echo $maxlength; ?>" class="textfield" <?php echo $chg_evt_handler; ?>="return unNullify('<?php echo urlencode($field); ?>')" tabindex="<?php echo $i+1; ?>" />
473 </td>
474 <?php
476 echo "\n";
478 </tr>
479 <?php
480 echo "\n";
481 } // end for
483 </table>
484 <br />
486 <table cellpadding="5">
487 <tr>
488 <td valign="middle" nowrap="nowrap">
489 <?php
490 if (isset($primary_key)) {
492 <input type="radio" name="submit_type" value="<?php echo $strSave; ?>" id="radio_submit_type_save" checked="checked" tabindex="<?php echo $fields_cnt+1; ?>" /><label for="radio_submit_type_save"><?php echo $strSave; ?></label><br />
493 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $strOr; ?><br />
494 <input type="radio" name="submit_type" value="<?php echo $strInsertAsNewRow; ?>" id="radio_submit_type_insert_as_new_row" tabindex="<?php echo $fields_cnt+2; ?>" /><label for="radio_submit_type_insert_as_new_row"><?php echo $strInsertAsNewRow; ?></label>
495 <?php
496 } else {
497 echo "\n";
499 <input type="hidden" name="submit_type" value="<?php echo $strInsertAsNewRow; ?>" tabindex="<?php echo $fields_cnt+3; ?>" />
500 <?php
501 echo ' ' . $strInsertAsNewRow . "\n";
503 echo "\n";
505 // Defines whether "insert a new row after the current insert" should be
506 // checked or not (keep this choice sticky)
507 $checked = (!empty($message)) ? ' checked="checked"' : '';
509 </td>
510 <td valign="middle">
511 &nbsp;&nbsp;&nbsp;<b>-- <?php echo $strAnd; ?> --</b>&nbsp;&nbsp;&nbsp;
512 </td>
513 <td valign="middle" nowrap="nowrap">
514 <input type="radio" name="after_insert" value="back" id="radio_after_insert_back" checked="checked" tabindex="<?php echo $fields_cnt+4; ?>" /><label for="radio_after_insert_back"><?php echo $strAfterInsertBack; ?></label><br />
515 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $strOr; ?><br />
516 <input type="radio" name="after_insert" value="new_insert" id="radio_after_insert_new_insert"<?php echo $checked; ?> tabindex="<?php echo $fields_cnt+5; ?>" /><label for="radio_after_insert_new_insert"><?php echo $strAfterInsertNewInsert; ?></label>
517 </td>
518 </tr>
520 <tr>
521 <td colspan="3" align="center" valign="middle">
522 <input type="submit" value="<?php echo $strGo; ?>" tabindex="<?php echo $fields_cnt+6; ?>" />
523 </td>
524 </tr>
525 </table>
527 </form>
530 <?php
532 * Displays the footer
534 echo "\n";
535 require('./footer.inc.php3');