ON DELETE ON UPDATE
[phpmyadmin/crack.git] / tbl_alter.php3
blobc6967f72bf6b1f9d341757f1136bfec2cefc1a7b
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Gets some core libraries
8 */
9 require('./libraries/grab_globals.lib.php3');
10 $js_to_run = 'functions.js';
11 include('./header.inc.php3');
13 // Check parameters
14 PMA_checkParameters(array('db', 'table'));
16 /**
17 * Defines the url to return to in case of error in a sql statement
19 $err_url = 'tbl_properties_structure.php3?' . PMA_generate_common_url($db, $table);
22 /**
23 * Modifications have been submitted -> updates the table
25 $abort = false;
26 if (isset($submit)) {
27 $field_cnt = count($field_orig);
28 for ($i = 0; $i < $field_cnt; $i++) {
29 if (PMA_MYSQL_INT_VERSION < 32306) {
30 PMA_checkReservedWords($field_name[$i], $err_url);
33 // Some fields have been urlencoded or double quotes have been translated
34 // to "&quot;" in tbl_properties.php3
35 $field_orig[$i] = urldecode($field_orig[$i]);
36 if (strcmp(str_replace('"', '&quot;', $field_orig[$i]), $field_name[$i]) == 0) {
37 $field_name[$i] = $field_orig[$i];
39 $field_default_orig[$i] = urldecode($field_default_orig[$i]);
40 if (strcmp(str_replace('"', '&quot;', $field_default_orig[$i]), $field_default[$i]) == 0) {
41 $field_default[$i] = $field_default_orig[$i];
43 $field_length_orig[$i] = urldecode($field_length_orig[$i]);
44 if (strcmp(str_replace('"', '&quot;', $field_length_orig[$i]), $field_length[$i]) == 0) {
45 $field_length[$i] = $field_length_orig[$i];
47 if (!isset($query)) {
48 $query = '';
49 } else {
50 $query .= ', CHANGE ';
52 $query .= PMA_backquote($field_orig[$i]) . ' ' . PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
53 // Some field types shouldn't have lengths
54 if ($field_length[$i] != ''
55 && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
56 $query .= '(' . $field_length[$i] . ')';
58 if ($field_attribute[$i] != '') {
59 $query .= ' ' . $field_attribute[$i];
60 } else if (PMA_MYSQL_INT_VERSION >= 40100 && $field_charset[$i] != '') {
61 $query .= ' CHARACTER SET ' . $field_charset[$i];
63 if ($field_default[$i] != '') {
64 if (strtoupper($field_default[$i]) == 'NULL') {
65 $query .= ' DEFAULT NULL';
66 } else {
67 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
70 if ($field_null[$i] != '') {
71 $query .= ' ' . $field_null[$i];
73 if ($field_extra[$i] != '') {
74 $query .= ' ' . $field_extra[$i];
76 } // end for
78 // To allow replication, we first select the db to use and then run queries
79 // on this db.
80 $sql_query = 'USE ' . PMA_backquote($db);
81 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
82 // Optimization fix - 2 May 2001 - Robbat2
83 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . $query;
84 $error_create = false;
85 $result = PMA_mysql_query($sql_query) or $error_create = true;
87 if ($error_create == false) {
88 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
89 $btnDrop = 'Fake';
91 // garvin: If comments were sent, enable relation stuff
92 require('./libraries/relation.lib.php3');
93 require('./libraries/transformations.lib.php3');
95 $cfgRelation = PMA_getRelationsParam();
97 // garvin: Update comment table, if a comment was set.
98 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
99 @reset($field_comments);
100 while(list($fieldindex, $fieldcomment) = each($field_comments)) {
101 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, $field_orig[$fieldindex]);
105 // garvin: Rename relations&display fields, if altered.
106 if (($cfgRelation['displaywork'] || $cfgRelation['relwork']) && isset($field_orig) && is_array($field_orig)) {
107 @reset($field_orig);
108 while(list($fieldindex, $fieldcontent) = each($field_orig)) {
109 if ($field_name[$fieldindex] != $fieldcontent) {
110 if ($cfgRelation['displaywork']) {
111 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
112 . ' SET display_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
113 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
114 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
115 . ' AND display_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
116 $tb_rs = PMA_query_as_cu($table_query);
117 unset($table_query);
118 unset($tb_rs);
121 if ($cfgRelation['relwork']) {
122 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
123 . ' SET master_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
124 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
125 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
126 . ' AND master_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
127 $tb_rs = PMA_query_as_cu($table_query);
128 unset($table_query);
129 unset($tb_rs);
131 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
132 . ' SET foreign_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
133 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
134 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
135 . ' AND foreign_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
136 $tb_rs = PMA_query_as_cu($table_query);
137 unset($table_query);
138 unset($tb_rs);
139 } // end if relwork
140 } // end if fieldname has changed
141 } // end while check fieldnames
142 } // end if relations/display has to be changed
144 // garvin: Update comment table for mime types [MIME]
145 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
146 @reset($field_mimetype);
147 while(list($fieldindex, $mimetype) = each($field_mimetype)) {
148 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
152 $active_page = 'tbl_properties_structure.php3';
153 include('./tbl_properties_structure.php3');
154 exit();
155 } else {
156 PMA_mysqlDie('', '', '', $err_url, FALSE);
157 // garvin: An error happened while inserting/updating a table definition.
158 // to prevent total loss of that data, we embed the form once again.
159 // The variable $regenerate will be used to restore data in tbl_properties.inc.php3
160 if (isset($orig_field)) {
161 $field = $orig_field;
164 $regenerate = true;
169 * No modifications yet required -> displays the table fields
171 if ($abort == FALSE) {
172 if (!isset($selected)) {
173 PMA_checkParameters(array('field'));
174 $selected[] = $field;
175 $selected_cnt = 1;
176 } else { // from a multiple submit
177 $selected_cnt = count($selected);
180 // TODO: optimize in case of multiple fields to modify
181 for ($i = 0; $i < $selected_cnt; $i++) {
182 if (!empty($submit_mult)) {
183 $field = PMA_sqlAddslashes(urldecode($selected[$i]), TRUE);
184 } else {
185 $field = PMA_sqlAddslashes($selected[$i], TRUE);
187 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . " LIKE '$field'";
188 $result = PMA_mysql_query($local_query) or PMA_mysqlDie('', $local_query, '', $err_url);
189 $fields_meta[] = PMA_mysql_fetch_array($result);
190 mysql_free_result($result);
193 $num_fields = count($fields_meta);
194 $action = 'tbl_alter.php3';
195 include('./tbl_properties.inc.php3');
200 * Displays the footer
202 require('./footer.inc.php3');