bug 617029 for Loic
[phpmyadmin/crack.git] / tbl_addfield.php3
blob0fe6ae1dbf21bb4b658a4085699874611a75a11f
1 <?php
2 /* $Id$ */
5 /**
6 * Get some core libraries
7 */
8 require('./libraries/grab_globals.lib.php3');
9 if (isset($submit)) {
10 $js_to_run = 'functions.js';
12 require('./header.inc.php3');
15 /**
16 * Defines the url to return to in case of error in a sql statement
18 $err_url = 'tbl_properties.php3'
19 . '?lang=' . $lang
20 . '&amp;convcharset=' . $convcharset
21 . '&amp;server=' . $server
22 . '&amp;db=' . urlencode($db)
23 . '&amp;table=' . urlencode($table);
26 /**
27 * The form used to define the field to add has been submitted
29 if (isset($submit)) {
30 $query = '';
32 // Transforms the radio button field_key into 3 arrays
33 $field_cnt = count($field_name);
34 for ($i = 0; $i < $field_cnt; ++$i) {
35 if (isset(${'field_key_' . $i})) {
36 if (${'field_key_' . $i} == 'primary_' . $i) {
37 $field_primary[] = $i;
39 if (${'field_key_' . $i} == 'index_' . $i) {
40 $field_index[] = $i;
42 if (${'field_key_' . $i} == 'unique_' . $i) {
43 $field_unique[] = $i;
45 } // end if
46 } // end for
47 // Builds the field creation statement and alters the table
48 for ($i = 0; $i < $field_cnt; ++$i) {
49 if (get_magic_quotes_gpc()) {
50 $field_name[$i] = stripslashes($field_name[$i]);
52 if (PMA_MYSQL_INT_VERSION < 32306) {
53 PMA_checkReservedWords($field_name[$i], $err_url);
56 $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
57 if ($field_length[$i] != ''
58 && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
59 if (get_magic_quotes_gpc()) {
60 $query .= '(' . stripslashes($field_length[$i]) . ')';
61 } else {
62 $query .= '(' . $field_length[$i] . ')';
65 if ($field_attribute[$i] != '') {
66 $query .= ' ' . $field_attribute[$i];
68 if ($field_default[$i] != '') {
69 if (strtoupper($field_default[$i]) == 'NULL') {
70 $query .= ' DEFAULT NULL';
71 } else if (get_magic_quotes_gpc()) {
72 $query .= ' DEFAULT \'' . PMA_sqlAddslashes(stripslashes($field_default[$i])) . '\'';
73 } else {
74 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
77 if ($field_null[$i] != '') {
78 $query .= ' ' . $field_null[$i];
80 if ($field_extra[$i] != '') {
81 $query .= ' ' . $field_extra[$i];
82 // An auto_increment field must be use as a primary key
83 if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
84 $primary_cnt = count($field_primary);
85 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
86 // void
87 } // end for
88 if ($field_primary[$j] == $i) {
89 $query .= ' PRIMARY KEY';
90 unset($field_primary[$j]);
91 } // end if
92 } // end if (auto_increment)
95 if ($after_field != '--end--') {
96 // Only the first field can be added somewhere else than at the end
97 if ($i == 0) {
98 if ($after_field == '--first--') {
99 $query .= ' FIRST';
100 } else {
101 if (get_magic_quotes_gpc()) {
102 $query .= ' AFTER ' . PMA_backquote(stripslashes(urldecode($after_field)));
103 } else {
104 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
107 } else {
108 if (get_magic_quotes_gpc()) {
109 $query .= ' AFTER ' . PMA_backquote(stripslashes($field_name[$i-1]));
110 } else {
111 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
115 $query .= ', ADD ';
116 } // end for
117 $query = ereg_replace(', ADD $', '', $query);
119 // To allow replication, we first select the db to use and then run queries
120 // on this db.
121 $sql_query = 'USE ' . PMA_backquote($db);
122 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
123 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
124 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
125 $sql_query_cpy = $sql_query . ';';
127 // Builds the primary keys statements and updates the table
128 $primary = '';
129 if (isset($field_primary)) {
130 $primary_cnt = count($field_primary);
131 for ($i = 0; $i < $primary_cnt; $i++) {
132 $j = $field_primary[$i];
133 $primary .= PMA_backquote($field_name[$j]) . ', ';
134 } // end for
135 $primary = ereg_replace(', $', '', $primary);
136 if (!empty($primary)) {
137 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')';
138 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
139 $sql_query_cpy .= "\n" . $sql_query . ';';
141 } // end if
143 // Builds the indexes statements and updates the table
144 $index = '';
145 if (isset($field_index)) {
146 $index_cnt = count($field_index);
147 for ($i = 0; $i < $index_cnt; $i++) {
148 $j = $field_index[$i];
149 $index .= PMA_backquote($field_name[$j]) . ', ';
150 } // end for
151 $index = ereg_replace(', $', '', $index);
152 if (!empty($index)) {
153 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
154 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
155 $sql_query_cpy .= "\n" . $sql_query . ';';
157 } // end if
159 // Builds the uniques statements and updates the table
160 $unique = '';
161 if (isset($field_unique)) {
162 $unique_cnt = count($field_unique);
163 for ($i = 0; $i < $unique_cnt; $i++) {
164 $j = $field_unique[$i];
165 $unique .= PMA_backquote($field_name[$j]) . ', ';
166 } // end for
167 $unique = ereg_replace(', $', '', $unique);
168 if (!empty($unique)) {
169 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
170 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
171 $sql_query_cpy .= "\n" . $sql_query . ';';
173 } // end if
176 // Builds the fulltext statements and updates the table
177 $fulltext = '';
178 if (PMA_MYSQL_INT_VERSION >= 32323 && isset($field_fulltext)) {
179 $fulltext_cnt = count($field_fulltext);
180 for ($i = 0; $i < $fulltext_cnt; $i++) {
181 $j = $field_fulltext[$i];
182 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
183 } // end for
184 $fulltext = ereg_replace(', $', '', $fulltext);
185 if (!empty($fulltext)) {
186 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
187 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
188 $sql_query_cpy .= "\n" . $sql_query . ';';
190 } // end if
192 // Go back to the structure sub-page
193 $sql_query = $sql_query_cpy;
194 unset($sql_query_cpy);
195 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
196 include('./tbl_properties_structure.php3');
197 exit();
198 } // end do alter table
201 * Displays the form used to define the new field
203 else{
204 $action = 'tbl_addfield.php3';
205 include('./tbl_properties.inc.php3');
207 // Diplays the footer
208 echo "\n";
209 include('./footer.inc.php3');