doc typos
[phpmyadmin/crack.git] / tbl_addfield.php
blob514d55c3285f9dc20fe57cc91abb5c543cadf738
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
9 * Get some core libraries
11 require_once './libraries/common.inc.php';
12 require_once './libraries/Table.class.php';
14 $GLOBALS['js_include'][] = 'functions.js';
15 require_once './libraries/header.inc.php';
17 // Check parameters
18 PMA_checkParameters(array('db', 'table'));
21 /**
22 * Defines the url to return to in case of error in a sql statement
24 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
26 /**
27 * The form used to define the field to add has been submitted
29 $abort = false;
31 // check number of fields to be created
32 if (isset($_REQUEST['submit_num_fields'])) {
33 if (isset($_REQUEST['orig_after_field'])) {
34 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
36 if (isset($_REQUEST['orig_field_where'])) {
37 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
39 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
40 $regenerate = true;
41 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
42 $num_fields = (int) $_REQUEST['num_fields'];
43 } else {
44 $num_fields = 1;
47 if (isset($_REQUEST['do_save_data'])) {
48 $query = '';
49 $definitions = array();
51 // Transforms the radio button field_key into 3 arrays
52 $field_cnt = count($_REQUEST['field_name']);
53 $field_primary = array();
54 $field_index = array();
55 $field_unique = array();
56 $field_fulltext = array();
57 for ($i = 0; $i < $field_cnt; ++$i) {
58 if (isset($_REQUEST['field_key'][$i])
59 && strlen($_REQUEST['field_name'][$i])) {
60 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
61 $field_primary[] = $i;
63 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
64 $field_index[] = $i;
66 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
67 $field_unique[] = $i;
69 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
70 $field_fulltext[] = $i;
72 } // end if
73 } // end for
75 // Builds the field creation statement and alters the table
76 for ($i = 0; $i < $field_cnt; ++$i) {
77 // '0' is also empty for php :-(
78 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
79 continue;
82 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
83 $_REQUEST['field_name'][$i],
84 $_REQUEST['field_type'][$i],
85 $_REQUEST['field_length'][$i],
86 $_REQUEST['field_attribute'][$i],
87 isset($_REQUEST['field_collation'][$i])
88 ? $_REQUEST['field_collation'][$i]
89 : '',
90 isset($_REQUEST['field_null'][$i])
91 ? $_REQUEST['field_null'][$i]
92 : 'NOT NULL',
93 $_REQUEST['field_default_type'][$i],
94 $_REQUEST['field_default_value'][$i],
95 isset($_REQUEST['field_extra'][$i])
96 ? $_REQUEST['field_extra'][$i]
97 : false,
98 isset($_REQUEST['field_comments'][$i])
99 ? $_REQUEST['field_comments'][$i]
100 : '',
101 $field_primary,
105 if ($_REQUEST['field_where'] != 'last') {
106 // Only the first field can be added somewhere other than at the end
107 if ($i == 0) {
108 if ($_REQUEST['field_where'] == 'first') {
109 $definition .= ' FIRST';
110 } else {
111 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
113 } else {
114 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
117 $definitions[] = $definition;
118 } // end for
120 // Builds the primary keys statements and updates the table
121 if (count($field_primary)) {
122 $fields = array();
123 foreach ($field_primary as $field_nr) {
124 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
126 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
127 unset($fields);
130 // Builds the indexes statements and updates the table
131 if (count($field_index)) {
132 $fields = array();
133 foreach ($field_index as $field_nr) {
134 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
136 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
137 unset($fields);
140 // Builds the uniques statements and updates the table
141 if (count($field_unique)) {
142 $fields = array();
143 foreach ($field_unique as $field_nr) {
144 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
146 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
147 unset($fields);
150 // Builds the fulltext statements and updates the table
151 if (count($field_fulltext)) {
152 $fields = array();
153 foreach ($field_fulltext as $field_nr) {
154 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
156 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
157 unset($fields);
160 // To allow replication, we first select the db to use and then run queries
161 // on this db.
162 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
163 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
164 $result = PMA_DBI_try_query($sql_query);
166 if ($result === true) {
167 // garvin: If comments were sent, enable relation stuff
168 require_once './libraries/relation.lib.php';
169 require_once './libraries/transformations.lib.php';
171 // garvin: Update comment table for mime types [MIME]
172 if (isset($_REQUEST['field_mimetype'])
173 && is_array($_REQUEST['field_mimetype'])
174 && $cfg['BrowseMIME']) {
175 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
176 if (isset($_REQUEST['field_name'][$fieldindex])
177 && strlen($_REQUEST['field_name'][$fieldindex])) {
178 PMA_setMIME($db, $table,
179 $_REQUEST['field_name'][$fieldindex],
180 $mimetype,
181 $_REQUEST['field_transformation'][$fieldindex],
182 $_REQUEST['field_transformation_options'][$fieldindex]);
187 // Go back to the structure sub-page
188 $message = PMA_Message::success('strTableAlteredSuccessfully');
189 $message->addParam($table);
190 $active_page = 'tbl_structure.php';
191 require './tbl_structure.php';
192 } else {
193 PMA_mysqlDie('', '', '', $err_url, false);
194 // garvin: An error happened while inserting/updating a table definition.
195 // to prevent total loss of that data, we embed the form once again.
196 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
197 $num_fields = $_REQUEST['orig_num_fields'];
198 if (isset($_REQUEST['orig_after_field'])) {
199 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
201 if (isset($_REQUEST['orig_field_where'])) {
202 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
204 $regenerate = true;
206 } // end do alter table
209 * Displays the form used to define the new field
211 if ($abort == false) {
213 * Gets tables informations
215 require_once './libraries/tbl_common.php';
216 require_once './libraries/tbl_info.inc.php';
218 * Displays top menu links
220 $active_page = 'tbl_structure.php';
221 require_once './libraries/tbl_links.inc.php';
223 * Display the form
225 $action = 'tbl_addfield.php';
226 require_once './libraries/tbl_properties.inc.php';
228 // Diplays the footer
229 require_once './libraries/footer.inc.php';