2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * @uses $cfg['DefaultTabDatabase']
5 * @uses $GLOBALS['table']
8 * @uses $strTableAlreadyExists
10 * @uses $strTableHasBeenCreated
11 * @uses PMA_Table::generateFieldSpec()
12 * @uses PMA_checkParameters()
13 * @uses PMA_generateCharsetQueryPart()
14 * @uses PMA_sqlAddslashes()
15 * @uses PMA_DBI_try_query()
16 * @uses PMA_getRelationsParam()
18 * @uses PMA_mysqlDie()
19 * @uses PMA_generate_common_url()
20 * @uses PMA_DBI_get_columns()
21 * @uses PMA_DBI_select_db()
22 * @uses PMA_backquote()
23 * @uses $_REQUEST['do_save_data']
24 * @uses $_REQUEST['submit_num_fields']
25 * @uses $_REQUEST['orig_num_fields']
26 * @uses $_REQUEST['added_fields']
27 * @uses $_REQUEST['num_fields']
28 * @uses preg_replace()
33 * @uses htmlspecialchars()
38 * Get some core libraries
40 require_once './libraries/common.inc.php';
41 require_once './libraries/Table.class.php';
43 $GLOBALS['js_include'][] = 'functions.js';
45 require_once './libraries/header.inc.php';
48 // @todo PMA_checkParameters does not check db and table proper with strlen()
49 PMA_checkParameters(array('db', 'table'));
52 * Defines the url to return to in case of error in a sql statement
54 if (! strlen($table)) {
56 PMA_mysqlDie($strTableEmpty, '', '',
57 'db_structure.php?' . PMA_generate_common_url($db));
58 } elseif (PMA_DBI_get_columns($db, $table)) {
59 // table exists already
60 PMA_mysqlDie(sprintf($strTableAlreadyExists, htmlspecialchars($table)), '',
61 '', 'db_structure.php?' . PMA_generate_common_url($db));
64 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
66 // check number of fields to be created
67 if (isset($_REQUEST['submit_num_fields'])) {
68 $regenerate = true; // for libraries/tbl_properties.inc.php
69 $num_fields = $_REQUEST['orig_num_fields'] +
$_REQUEST['added_fields'];
70 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
71 $num_fields = (int) $_REQUEST['num_fields'];
77 * Selects the database to work with
79 PMA_DBI_select_db($db);
82 * The form used to define the structure of the table has been submitted
84 if (isset($_REQUEST['do_save_data'])) {
87 // Transforms the radio button field_key into 3 arrays
88 $field_cnt = count($_REQUEST['field_name']);
89 for ($i = 0; $i < $field_cnt; ++
$i) {
90 if (isset($_REQUEST['field_key'][$i])) {
91 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
92 $field_primary[] = $i;
94 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
97 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
103 // Builds the fields creation statements
104 for ($i = 0; $i < $field_cnt; $i++
) {
105 // '0' is also empty for php :-(
106 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
110 $query = PMA_Table
::generateFieldSpec(
111 $_REQUEST['field_name'][$i],
112 $_REQUEST['field_type'][$i],
113 $_REQUEST['field_length'][$i],
114 $_REQUEST['field_attribute'][$i],
115 isset($_REQUEST['field_collation'][$i])
116 ?
$_REQUEST['field_collation'][$i]
118 isset($_REQUEST['field_null'][$i])
119 ?
$_REQUEST['field_null'][$i]
121 $_REQUEST['field_default_type'][$i],
122 $_REQUEST['field_default_value'][$i],
123 isset($_REQUEST['field_extra'][$i])
124 ?
$_REQUEST['field_extra'][$i]
126 isset($_REQUEST['field_comments'][$i])
127 ?
$_REQUEST['field_comments'][$i]
133 $sql_query .= $query;
135 unset($field_cnt, $query);
136 $sql_query = preg_replace('@, $@', '', $sql_query);
138 // Builds the primary keys statements
140 $primary_cnt = (isset($field_primary) ?
count($field_primary) : 0);
141 for ($i = 0; $i < $primary_cnt; $i++
) {
142 $j = $field_primary[$i];
143 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
144 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
148 $primary = preg_replace('@, $@', '', $primary);
149 if (strlen($primary)) {
150 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
154 // Builds the indexes statements
156 $index_cnt = (isset($field_index) ?
count($field_index) : 0);
157 for ($i = 0;$i < $index_cnt; $i++
) {
158 $j = $field_index[$i];
159 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
160 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
164 $index = preg_replace('@, $@', '', $index);
165 if (strlen($index)) {
166 $sql_query .= ', INDEX (' . $index . ')';
170 // Builds the uniques statements
172 $unique_cnt = (isset($field_unique) ?
count($field_unique) : 0);
173 for ($i = 0; $i < $unique_cnt; $i++
) {
174 $j = $field_unique[$i];
175 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
176 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
180 $unique = preg_replace('@, $@', '', $unique);
181 if (strlen($unique)) {
182 $sql_query .= ', UNIQUE (' . $unique . ')';
186 // Builds the FULLTEXT statements
188 $fulltext_cnt = (isset($field_fulltext) ?
count($field_fulltext) : 0);
189 for ($i = 0; $i < $fulltext_cnt; $i++
) {
190 $j = $field_fulltext[$i];
191 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
192 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
196 $fulltext = preg_replace('@, $@', '', $fulltext);
197 if (strlen($fulltext)) {
198 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
202 // Builds the 'create table' statement
203 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
204 . ' (' . $sql_query . ')';
206 // Adds table type, character set, comments and partition definition
207 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
208 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
210 if (!empty($_REQUEST['tbl_collation'])) {
211 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
213 if (!empty($_REQUEST['comment'])) {
214 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\'';
216 if (!empty($_REQUEST['partition_definition'])) {
217 $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']);
220 // Executes the query
221 $result = PMA_DBI_try_query($sql_query);
225 // garvin: If comments were sent, enable relation stuff
226 require_once './libraries/relation.lib.php';
227 require_once './libraries/transformations.lib.php';
229 // garvin: Update comment table for mime types [MIME]
230 if (isset($_REQUEST['field_mimetype'])
231 && is_array($_REQUEST['field_mimetype'])
232 && $cfg['BrowseMIME']) {
233 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
234 if (isset($_REQUEST['field_name'][$fieldindex])
235 && strlen($_REQUEST['field_name'][$fieldindex])) {
236 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
237 $_REQUEST['field_transformation'][$fieldindex],
238 $_REQUEST['field_transformation_options'][$fieldindex]);
243 $message = PMA_Message
::success('strTableHasBeenCreated');
244 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
246 $display_query = $sql_query;
249 // do not switch to sql.php - as there is no row to be displayed on a new table
250 if ($cfg['DefaultTabTable'] === 'sql.php') {
251 require './tbl_structure.php';
253 require './' . $cfg['DefaultTabTable'];
257 PMA_mysqlDie('', '', '', $err_url, false);
258 // garvin: An error happened while inserting/updating a table definition.
259 // to prevent total loss of that data, we embed the form once again.
260 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
261 $num_fields = $_REQUEST['orig_num_fields'];
264 } // end do create table
267 * Displays the form used to define the structure of the table
269 $action = 'tbl_create.php';
270 require './libraries/tbl_properties.inc.php';
271 // Displays the footer
272 require_once './libraries/footer.inc.php';