Convert documentation to similar style as our web site uses.
[phpmyadmin/crack.git] / tbl_addfield.php
blob8d0e998ee810e6f2333ad18e14f2af98606e55bb
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 for ($i = 0; $i < $field_cnt; ++$i) {
57 if (isset($_REQUEST['field_key'][$i])
58 && strlen($_REQUEST['field_name'][$i])) {
59 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
60 $field_primary[] = $i;
62 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
63 $field_index[] = $i;
65 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
66 $field_unique[] = $i;
68 } // end if
69 } // end for
71 // Builds the field creation statement and alters the table
72 for ($i = 0; $i < $field_cnt; ++$i) {
73 // '0' is also empty for php :-(
74 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
75 continue;
78 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
79 $_REQUEST['field_name'][$i],
80 $_REQUEST['field_type'][$i],
81 $_REQUEST['field_length'][$i],
82 $_REQUEST['field_attribute'][$i],
83 isset($_REQUEST['field_collation'][$i])
84 ? $_REQUEST['field_collation'][$i]
85 : '',
86 isset($_REQUEST['field_null'][$i])
87 ? $_REQUEST['field_null'][$i]
88 : 'NOT NULL',
89 $_REQUEST['field_default_type'][$i],
90 $_REQUEST['field_default_value'][$i],
91 isset($_REQUEST['field_extra'][$i])
92 ? $_REQUEST['field_extra'][$i]
93 : false,
94 isset($_REQUEST['field_comments'][$i])
95 ? $_REQUEST['field_comments'][$i]
96 : '',
97 $field_primary,
101 if ($_REQUEST['field_where'] != 'last') {
102 // Only the first field can be added somewhere other than at the end
103 if ($i == 0) {
104 if ($_REQUEST['field_where'] == 'first') {
105 $definition .= ' FIRST';
106 } else {
107 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
109 } else {
110 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
113 $definitions[] = $definition;
114 } // end for
116 // Builds the primary keys statements and updates the table
117 if (count($field_primary)) {
118 $fields = array();
119 foreach ($field_primary as $field_nr) {
120 $fields[] = $_REQUEST['field_name'][$field_nr];
122 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
125 // Builds the indexes statements and updates the table
126 if (count($field_index)) {
127 $fields = array();
128 foreach ($field_index as $field_nr) {
129 $fields[] = $_REQUEST['field_name'][$field_nr];
131 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
134 // Builds the uniques statements and updates the table
135 if (count($field_unique)) {
136 $fields = array();
137 foreach ($field_unique as $field_nr) {
138 $fields[] = $_REQUEST['field_name'][$field_nr];
140 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
143 // Builds the fulltext statements and updates the table
144 if (isset($field_fulltext) && count($field_fulltext)) {
145 $fields = array();
146 foreach ($field_fulltext as $field_nr) {
147 $fields[] = $_REQUEST['field_name'][$field_nr];
149 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
152 // To allow replication, we first select the db to use and then run queries
153 // on this db.
154 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
155 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
156 $result = PMA_DBI_try_query($sql_query);
158 if ($result === true) {
159 // garvin: If comments were sent, enable relation stuff
160 require_once './libraries/relation.lib.php';
161 require_once './libraries/transformations.lib.php';
163 // garvin: Update comment table for mime types [MIME]
164 if (isset($_REQUEST['field_mimetype'])
165 && is_array($_REQUEST['field_mimetype'])
166 && $cfg['BrowseMIME']) {
167 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
168 if (isset($_REQUEST['field_name'][$fieldindex])
169 && strlen($_REQUEST['field_name'][$fieldindex])) {
170 PMA_setMIME($db, $table,
171 $_REQUEST['field_name'][$fieldindex],
172 $mimetype,
173 $_REQUEST['field_transformation'][$fieldindex],
174 $_REQUEST['field_transformation_options'][$fieldindex]);
179 // Go back to the structure sub-page
180 $message = PMA_Message::success('strTableAlteredSuccessfully');
181 $message->addParam($table);
182 $active_page = 'tbl_structure.php';
183 require './tbl_structure.php';
184 } else {
185 PMA_mysqlDie('', '', '', $err_url, false);
186 // garvin: An error happened while inserting/updating a table definition.
187 // to prevent total loss of that data, we embed the form once again.
188 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
189 $num_fields = $_REQUEST['orig_num_fields'];
190 if (isset($_REQUEST['orig_after_field'])) {
191 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
193 if (isset($_REQUEST['orig_field_where'])) {
194 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
196 $regenerate = true;
198 } // end do alter table
201 * Displays the form used to define the new field
203 if ($abort == false) {
205 * Gets tables informations
207 require_once './libraries/tbl_common.php';
208 require_once './libraries/tbl_info.inc.php';
210 * Displays top menu links
212 $active_page = 'tbl_structure.php';
213 require_once './libraries/tbl_links.inc.php';
215 * Display the form
217 $action = 'tbl_addfield.php';
218 require_once './libraries/tbl_properties.inc.php';
220 // Diplays the footer
221 require_once './libraries/footer.inc.php';