update
[phpmyadmin/crack.git] / tbl_addfield.php3
blob5182937bab70367ff95860caac790e872ce2edf4
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Get some core libraries
8 */
9 require('./libraries/grab_globals.lib.php3');
10 $js_to_run = 'functions.js';
11 require('./header.inc.php3');
14 /**
15 * Defines the url to return to in case of error in a sql statement
17 $err_url = 'tbl_properties.php3?' . PMA_generate_common_url($db, $table);
20 /**
21 * The form used to define the field to add has been submitted
23 if (isset($submit)) {
24 $query = '';
26 // Transforms the radio button field_key into 3 arrays
27 $field_cnt = count($field_name);
28 for ($i = 0; $i < $field_cnt; ++$i) {
29 if (isset(${'field_key_' . $i})) {
30 if (${'field_key_' . $i} == 'primary_' . $i) {
31 $field_primary[] = $i;
33 if (${'field_key_' . $i} == 'index_' . $i) {
34 $field_index[] = $i;
36 if (${'field_key_' . $i} == 'unique_' . $i) {
37 $field_unique[] = $i;
39 } // end if
40 } // end for
41 // Builds the field creation statement and alters the table
42 for ($i = 0; $i < $field_cnt; ++$i) {
43 if (empty($field_name[$i])) {
44 continue;
46 if (get_magic_quotes_gpc()) {
47 $field_name[$i] = stripslashes($field_name[$i]);
49 if (PMA_MYSQL_INT_VERSION < 32306) {
50 PMA_checkReservedWords($field_name[$i], $err_url);
53 $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
54 if ($field_length[$i] != ''
55 && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
56 if (get_magic_quotes_gpc()) {
57 $query .= '(' . stripslashes($field_length[$i]) . ')';
58 } else {
59 $query .= '(' . $field_length[$i] . ')';
62 if ($field_attribute[$i] != '') {
63 $query .= ' ' . $field_attribute[$i];
65 if ($field_default[$i] != '') {
66 if (strtoupper($field_default[$i]) == 'NULL') {
67 $query .= ' DEFAULT NULL';
68 } else if (get_magic_quotes_gpc()) {
69 $query .= ' DEFAULT \'' . PMA_sqlAddslashes(stripslashes($field_default[$i])) . '\'';
70 } else {
71 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
74 if ($field_null[$i] != '') {
75 $query .= ' ' . $field_null[$i];
77 if ($field_extra[$i] != '') {
78 $query .= ' ' . $field_extra[$i];
79 // An auto_increment field must be use as a primary key
80 if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
81 $primary_cnt = count($field_primary);
82 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
83 // void
84 } // end for
85 if ($field_primary[$j] == $i) {
86 $query .= ' PRIMARY KEY';
87 unset($field_primary[$j]);
88 } // end if
89 } // end if (auto_increment)
92 if ($after_field != '--end--') {
93 // Only the first field can be added somewhere else than at the end
94 if ($i == 0) {
95 if ($after_field == '--first--') {
96 $query .= ' FIRST';
97 } else {
98 if (get_magic_quotes_gpc()) {
99 $query .= ' AFTER ' . PMA_backquote(stripslashes(urldecode($after_field)));
100 } else {
101 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
104 } else {
105 if (get_magic_quotes_gpc()) {
106 $query .= ' AFTER ' . PMA_backquote(stripslashes($field_name[$i-1]));
107 } else {
108 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
112 $query .= ', ADD ';
113 } // end for
114 $query = ereg_replace(', ADD $', '', $query);
116 // To allow replication, we first select the db to use and then run queries
117 // on this db.
118 $sql_query = 'USE ' . PMA_backquote($db);
119 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
120 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
121 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
122 $sql_query_cpy = $sql_query . ';';
124 // Builds the primary keys statements and updates the table
125 $primary = '';
126 if (isset($field_primary)) {
127 $primary_cnt = count($field_primary);
128 for ($i = 0; $i < $primary_cnt; $i++) {
129 $j = $field_primary[$i];
130 if (!empty($field_name[$j])) {
131 $primary .= PMA_backquote($field_name[$j]) . ', ';
133 } // end for
134 $primary = ereg_replace(', $', '', $primary);
135 if (!empty($primary)) {
136 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')';
137 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
138 $sql_query_cpy .= "\n" . $sql_query . ';';
140 } // end if
142 // Builds the indexes statements and updates the table
143 $index = '';
144 if (isset($field_index)) {
145 $index_cnt = count($field_index);
146 for ($i = 0; $i < $index_cnt; $i++) {
147 $j = $field_index[$i];
148 if (!empty($field_name[$j])) {
149 $index .= PMA_backquote($field_name[$j]) . ', ';
151 } // end for
152 $index = ereg_replace(', $', '', $index);
153 if (!empty($index)) {
154 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
155 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
156 $sql_query_cpy .= "\n" . $sql_query . ';';
158 } // end if
160 // Builds the uniques statements and updates the table
161 $unique = '';
162 if (isset($field_unique)) {
163 $unique_cnt = count($field_unique);
164 for ($i = 0; $i < $unique_cnt; $i++) {
165 $j = $field_unique[$i];
166 if (!empty($field_name[$j])) {
167 $unique .= PMA_backquote($field_name[$j]) . ', ';
169 } // end for
170 $unique = ereg_replace(', $', '', $unique);
171 if (!empty($unique)) {
172 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
173 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
174 $sql_query_cpy .= "\n" . $sql_query . ';';
176 } // end if
179 // Builds the fulltext statements and updates the table
180 $fulltext = '';
181 if (PMA_MYSQL_INT_VERSION >= 32323 && isset($field_fulltext)) {
182 $fulltext_cnt = count($field_fulltext);
183 for ($i = 0; $i < $fulltext_cnt; $i++) {
184 $j = $field_fulltext[$i];
185 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
186 } // end for
187 $fulltext = ereg_replace(', $', '', $fulltext);
188 if (!empty($fulltext)) {
189 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
190 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
191 $sql_query_cpy .= "\n" . $sql_query . ';';
193 } // end if
195 // Go back to the structure sub-page
196 $sql_query = $sql_query_cpy;
197 unset($sql_query_cpy);
198 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
199 include('./tbl_properties_structure.php3');
200 exit();
201 } // end do alter table
204 * Displays the form used to define the new field
206 else{
207 $action = 'tbl_addfield.php3';
208 include('./tbl_properties.inc.php3');
210 // Diplays the footer
211 echo "\n";
212 include('./footer.inc.php3');