Merge remote branch 'origin/master'
[phpmyadmin/dkf.git] / tbl_create.php
blob4f6b56f638525de560329e1c4e86fb96c61181cd
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @uses $cfg['DefaultTabDatabase']
5 * @uses $GLOBALS['table']
6 * @uses $GLOBALS['db']
7 * @uses PMA_Table::generateFieldSpec()
8 * @uses PMA_checkParameters()
9 * @uses PMA_generateCharsetQueryPart()
10 * @uses PMA_sqlAddslashes()
11 * @uses PMA_DBI_try_query()
12 * @uses PMA_getRelationsParam()
13 * @uses PMA_setMIME()
14 * @uses PMA_mysqlDie()
15 * @uses PMA_generate_common_url()
16 * @uses PMA_DBI_get_columns()
17 * @uses PMA_DBI_select_db()
18 * @uses PMA_backquote()
19 * @uses $_REQUEST['do_save_data']
20 * @uses $_REQUEST['submit_num_fields']
21 * @uses $_REQUEST['orig_num_fields']
22 * @uses $_REQUEST['added_fields']
23 * @uses $_REQUEST['num_fields']
24 * @uses preg_replace()
25 * @uses count()
26 * @uses is_array()
27 * @uses strlen()
28 * @uses sprintf()
29 * @uses htmlspecialchars()
30 * @package phpMyAdmin
33 /**
34 * Get some core libraries
36 require_once './libraries/common.inc.php';
38 $action = 'tbl_create.php';
40 require_once './libraries/header.inc.php';
42 // Check parameters
43 PMA_checkParameters(array('db'));
45 /* Check if database name is empty */
46 if (strlen($db) == 0) {
47 PMA_mysqlDie(__('The database name is empty!'), '', '', 'main.php');
50 /**
51 * Defines the url to return to in case of error in a sql statement
53 if (PMA_DBI_get_columns($db, $table)) {
54 // table exists already
55 PMA_mysqlDie(sprintf(__('Table %s already exists!'), htmlspecialchars($table)), '',
56 '', 'db_structure.php?' . PMA_generate_common_url($db));
59 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
61 // check number of fields to be created
62 if (isset($_REQUEST['submit_num_fields'])) {
63 $regenerate = true; // for libraries/tbl_properties.inc.php
64 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
65 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
66 $num_fields = (int) $_REQUEST['num_fields'];
67 } else {
68 $num_fields = 2;
71 /**
72 * Selects the database to work with
74 if (!PMA_DBI_select_db($db)) {
75 PMA_mysqlDie(sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
76 '', '', 'main.php');
79 /**
80 * The form used to define the structure of the table has been submitted
82 if (isset($_REQUEST['do_save_data'])) {
83 $sql_query = '';
85 // Transforms the radio button field_key into 3 arrays
86 $field_cnt = count($_REQUEST['field_name']);
87 for ($i = 0; $i < $field_cnt; ++$i) {
88 if (isset($_REQUEST['field_key'][$i])) {
89 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
90 $field_primary[] = $i;
92 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
93 $field_index[] = $i;
95 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
96 $field_unique[] = $i;
98 } // end if
99 } // end for
101 // Builds the fields creation statements
102 for ($i = 0; $i < $field_cnt; $i++) {
103 // '0' is also empty for php :-(
104 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
105 continue;
108 $query = PMA_Table::generateFieldSpec(
109 $_REQUEST['field_name'][$i],
110 $_REQUEST['field_type'][$i],
111 $_REQUEST['field_length'][$i],
112 $_REQUEST['field_attribute'][$i],
113 isset($_REQUEST['field_collation'][$i])
114 ? $_REQUEST['field_collation'][$i]
115 : '',
116 isset($_REQUEST['field_null'][$i])
117 ? $_REQUEST['field_null'][$i]
118 : 'NOT NULL',
119 $_REQUEST['field_default_type'][$i],
120 $_REQUEST['field_default_value'][$i],
121 isset($_REQUEST['field_extra'][$i])
122 ? $_REQUEST['field_extra'][$i]
123 : false,
124 isset($_REQUEST['field_comments'][$i])
125 ? $_REQUEST['field_comments'][$i]
126 : '',
127 $field_primary,
128 $i);
130 $query .= ', ';
131 $sql_query .= $query;
132 } // end for
133 unset($field_cnt, $query);
134 $sql_query = preg_replace('@, $@', '', $sql_query);
136 // Builds the primary keys statements
137 $primary = '';
138 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
139 for ($i = 0; $i < $primary_cnt; $i++) {
140 $j = $field_primary[$i];
141 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
142 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
144 } // end for
145 unset($primary_cnt);
146 $primary = preg_replace('@, $@', '', $primary);
147 if (strlen($primary)) {
148 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
150 unset($primary);
152 // Builds the indexes statements
153 $index = '';
154 $index_cnt = (isset($field_index) ? count($field_index) : 0);
155 for ($i = 0;$i < $index_cnt; $i++) {
156 $j = $field_index[$i];
157 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
158 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
160 } // end for
161 unset($index_cnt);
162 $index = preg_replace('@, $@', '', $index);
163 if (strlen($index)) {
164 $sql_query .= ', INDEX (' . $index . ')';
166 unset($index);
168 // Builds the uniques statements
169 $unique = '';
170 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
171 for ($i = 0; $i < $unique_cnt; $i++) {
172 $j = $field_unique[$i];
173 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
174 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
176 } // end for
177 unset($unique_cnt);
178 $unique = preg_replace('@, $@', '', $unique);
179 if (strlen($unique)) {
180 $sql_query .= ', UNIQUE (' . $unique . ')';
182 unset($unique);
184 // Builds the FULLTEXT statements
185 $fulltext = '';
186 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
187 for ($i = 0; $i < $fulltext_cnt; $i++) {
188 $j = $field_fulltext[$i];
189 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
190 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
192 } // end for
194 $fulltext = preg_replace('@, $@', '', $fulltext);
195 if (strlen($fulltext)) {
196 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
198 unset($fulltext);
200 // Builds the 'create table' statement
201 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
202 . ' (' . $sql_query . ')';
204 // Adds table type, character set, comments and partition definition
205 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
206 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
208 if (!empty($_REQUEST['tbl_collation'])) {
209 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
211 if (!empty($_REQUEST['comment'])) {
212 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\'';
214 if (!empty($_REQUEST['partition_definition'])) {
215 $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']);
217 $sql_query .= ';';
219 // Executes the query
220 $result = PMA_DBI_try_query($sql_query);
222 if ($result) {
224 // If comments were sent, enable relation stuff
225 require_once './libraries/transformations.lib.php';
227 // Update comment table for mime types [MIME]
228 if (isset($_REQUEST['field_mimetype'])
229 && is_array($_REQUEST['field_mimetype'])
230 && $cfg['BrowseMIME']) {
231 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
232 if (isset($_REQUEST['field_name'][$fieldindex])
233 && strlen($_REQUEST['field_name'][$fieldindex])) {
234 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
235 $_REQUEST['field_transformation'][$fieldindex],
236 $_REQUEST['field_transformation_options'][$fieldindex]);
241 $message = PMA_Message::success(__('Table %1$s has been created.'));
242 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
244 $display_query = $sql_query;
245 $sql_query = '';
247 // read table info on this newly created table, in case
248 // the next page is Structure
249 $reread_info = true;
250 require './libraries/tbl_info.inc.php';
252 // do not switch to sql.php - as there is no row to be displayed on a new table
253 if ($cfg['DefaultTabTable'] === 'sql.php') {
254 require './tbl_structure.php';
255 } else {
256 require './' . $cfg['DefaultTabTable'];
258 exit;
259 } else {
260 PMA_mysqlDie('', '', '', $err_url, false);
261 // An error happened while inserting/updating a table definition.
262 // to prevent total loss of that data, we embed the form once again.
263 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
264 $num_fields = $_REQUEST['orig_num_fields'];
265 $regenerate = true;
267 } // end do create table
270 * Displays the form used to define the structure of the table
272 require './libraries/tbl_properties.inc.php';
273 // Displays the footer
274 require './libraries/footer.inc.php';