problem showing UNHEX function by default when editing a BLOB was not fixed correctly...
[phpmyadmin/tyronm.git] / tbl_addfield.php
blobcfa25f5496249382796bc53c170b819829faefd2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 /**
10 * Get some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Table.class.php';
15 $GLOBALS['js_include'][] = 'functions.js';
16 $GLOBALS['js_include'][] = 'mootools.js';
17 require_once './libraries/header.inc.php';
19 // Check parameters
20 PMA_checkParameters(array('db', 'table'));
23 /**
24 * Defines the url to return to in case of error in a sql statement
26 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
28 /**
29 * The form used to define the field to add has been submitted
31 $abort = false;
33 // check number of fields to be created
34 if (isset($_REQUEST['submit_num_fields'])) {
35 if (isset($_REQUEST['orig_after_field'])) {
36 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
38 if (isset($_REQUEST['orig_field_where'])) {
39 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
41 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
42 $regenerate = true;
43 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
44 $num_fields = (int) $_REQUEST['num_fields'];
45 } else {
46 $num_fields = 1;
49 if (isset($_REQUEST['do_save_data'])) {
50 $query = '';
51 $definitions = array();
53 // Transforms the radio button field_key into 3 arrays
54 $field_cnt = count($_REQUEST['field_name']);
55 $field_primary = array();
56 $field_index = array();
57 $field_unique = array();
58 $field_fulltext = array();
59 for ($i = 0; $i < $field_cnt; ++$i) {
60 if (isset($_REQUEST['field_key'][$i])
61 && strlen($_REQUEST['field_name'][$i])) {
62 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
63 $field_primary[] = $i;
65 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
66 $field_index[] = $i;
68 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
69 $field_unique[] = $i;
71 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
72 $field_fulltext[] = $i;
74 } // end if
75 } // end for
77 // Builds the field creation statement and alters the table
78 for ($i = 0; $i < $field_cnt; ++$i) {
79 // '0' is also empty for php :-(
80 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
81 continue;
84 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
85 $_REQUEST['field_name'][$i],
86 $_REQUEST['field_type'][$i],
87 $_REQUEST['field_length'][$i],
88 $_REQUEST['field_attribute'][$i],
89 isset($_REQUEST['field_collation'][$i])
90 ? $_REQUEST['field_collation'][$i]
91 : '',
92 isset($_REQUEST['field_null'][$i])
93 ? $_REQUEST['field_null'][$i]
94 : 'NOT NULL',
95 $_REQUEST['field_default_type'][$i],
96 $_REQUEST['field_default_value'][$i],
97 isset($_REQUEST['field_extra'][$i])
98 ? $_REQUEST['field_extra'][$i]
99 : false,
100 isset($_REQUEST['field_comments'][$i])
101 ? $_REQUEST['field_comments'][$i]
102 : '',
103 $field_primary,
107 if ($_REQUEST['field_where'] != 'last') {
108 // Only the first field can be added somewhere other than at the end
109 if ($i == 0) {
110 if ($_REQUEST['field_where'] == 'first') {
111 $definition .= ' FIRST';
112 } else {
113 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
115 } else {
116 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
119 $definitions[] = $definition;
120 } // end for
122 // Builds the primary keys statements and updates the table
123 if (count($field_primary)) {
124 $fields = array();
125 foreach ($field_primary as $field_nr) {
126 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
128 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
129 unset($fields);
132 // Builds the indexes statements and updates the table
133 if (count($field_index)) {
134 $fields = array();
135 foreach ($field_index as $field_nr) {
136 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
138 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
139 unset($fields);
142 // Builds the uniques statements and updates the table
143 if (count($field_unique)) {
144 $fields = array();
145 foreach ($field_unique as $field_nr) {
146 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
148 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
149 unset($fields);
152 // Builds the fulltext statements and updates the table
153 if (count($field_fulltext)) {
154 $fields = array();
155 foreach ($field_fulltext as $field_nr) {
156 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
158 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
159 unset($fields);
162 // To allow replication, we first select the db to use and then run queries
163 // on this db.
164 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
165 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
166 $result = PMA_DBI_try_query($sql_query);
168 if ($result === true) {
169 // garvin: If comments were sent, enable relation stuff
170 require_once './libraries/relation.lib.php';
171 require_once './libraries/transformations.lib.php';
173 // garvin: Update comment table for mime types [MIME]
174 if (isset($_REQUEST['field_mimetype'])
175 && is_array($_REQUEST['field_mimetype'])
176 && $cfg['BrowseMIME']) {
177 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
178 if (isset($_REQUEST['field_name'][$fieldindex])
179 && strlen($_REQUEST['field_name'][$fieldindex])) {
180 PMA_setMIME($db, $table,
181 $_REQUEST['field_name'][$fieldindex],
182 $mimetype,
183 $_REQUEST['field_transformation'][$fieldindex],
184 $_REQUEST['field_transformation_options'][$fieldindex]);
189 // Go back to the structure sub-page
190 $message = PMA_Message::success('strTableAlteredSuccessfully');
191 $message->addParam($table);
192 $active_page = 'tbl_structure.php';
193 require './tbl_structure.php';
194 } else {
195 PMA_mysqlDie('', '', '', $err_url, false);
196 // garvin: An error happened while inserting/updating a table definition.
197 // to prevent total loss of that data, we embed the form once again.
198 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
199 $num_fields = $_REQUEST['orig_num_fields'];
200 if (isset($_REQUEST['orig_after_field'])) {
201 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
203 if (isset($_REQUEST['orig_field_where'])) {
204 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
206 $regenerate = true;
208 } // end do alter table
211 * Displays the form used to define the new field
213 if ($abort == false) {
215 * Gets tables informations
217 require_once './libraries/tbl_common.php';
218 require_once './libraries/tbl_info.inc.php';
220 * Displays top menu links
222 $active_page = 'tbl_structure.php';
223 require_once './libraries/tbl_links.inc.php';
225 * Display the form
227 $action = 'tbl_addfield.php';
228 require_once './libraries/tbl_properties.inc.php';
230 // Diplays the footer
231 require_once './libraries/footer.inc.php';