Fix typo.
[phpmyadmin/last10db.git] / tbl_create.php
blobdf9aa8e20c1fd0a00249a4c81dd5cb51f30fce15
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Get some core libraries
7 */
8 require_once './libraries/common.lib.php';
9 require_once './libraries/Table.class.php';
11 $js_to_run = 'functions.js';
13 if (isset($table)) {
14 $table = PMA_sanitize($table);
17 require_once './libraries/header.inc.php';
19 // Check parameters
20 PMA_checkParameters(array('db', 'table'));
22 /**
23 * Defines the url to return to in case of error in a sql statement
25 $err_url = $cfg['DefaultTabTable'] . '?' . PMA_generate_common_url($db, $table);
28 /**
29 * Selects the database to work with
31 PMA_DBI_select_db($db);
34 /**
35 * The form used to define the structure of the table has been submitted
37 $abort = false;
38 if (isset($submit_num_fields)) {
39 $regenerate = true;
40 $num_fields = $orig_num_fields + $added_fields;
41 } elseif (isset($do_save_data)) {
42 $sql_query = $query_cpy = '';
44 // Transforms the radio button field_key into 3 arrays
45 $field_cnt = count($field_name);
46 for ($i = 0; $i < $field_cnt; ++$i) {
47 if (isset(${'field_key_' . $i})) {
48 if (${'field_key_' . $i} == 'primary_' . $i) {
49 $field_primary[] = $i;
51 if (${'field_key_' . $i} == 'index_' . $i) {
52 $field_index[] = $i;
54 if (${'field_key_' . $i} == 'unique_' . $i) {
55 $field_unique[] = $i;
57 } // end if
58 } // end for
59 // Builds the fields creation statements
60 for ($i = 0; $i < $field_cnt; $i++) {
61 // '0' is also empty for php :-(
62 if (empty($field_name[$i]) && $field_name[$i] != '0') {
63 continue;
66 $query = PMA_Table::generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i);
68 $query .= ', ';
69 $sql_query .= $query;
70 $query_cpy .= "\n" . ' ' . $query;
71 } // end for
72 unset($field_cnt);
73 unset($query);
74 $sql_query = preg_replace('@, $@', '', $sql_query);
75 $query_cpy = preg_replace('@, $@', '', $query_cpy);
77 // Builds the primary keys statements
78 $primary = '';
79 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
80 for ($i = 0; $i < $primary_cnt; $i++) {
81 $j = $field_primary[$i];
82 if (isset($field_name[$j]) && strlen($field_name[$j])) {
83 $primary .= PMA_backquote($field_name[$j]) . ', ';
85 } // end for
86 unset($primary_cnt);
87 $primary = preg_replace('@, $@', '', $primary);
88 if (strlen($primary)) {
89 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
90 $query_cpy .= ',' . "\n" . ' PRIMARY KEY (' . $primary . ')';
92 unset($primary);
94 // Builds the indexes statements
95 $index = '';
96 $index_cnt = (isset($field_index) ? count($field_index) : 0);
97 for ($i = 0;$i < $index_cnt; $i++) {
98 $j = $field_index[$i];
99 if (isset($field_name[$j]) && strlen($field_name[$j])) {
100 $index .= PMA_backquote($field_name[$j]) . ', ';
102 } // end for
103 unset($index_cnt);
104 $index = preg_replace('@, $@', '', $index);
105 if (strlen($index)) {
106 $sql_query .= ', INDEX (' . $index . ')';
107 $query_cpy .= ',' . "\n" . ' INDEX (' . $index . ')';
109 unset($index);
111 // Builds the uniques statements
112 $unique = '';
113 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
114 for ($i = 0; $i < $unique_cnt; $i++) {
115 $j = $field_unique[$i];
116 if (isset($field_name[$j]) && strlen($field_name[$j])) {
117 $unique .= PMA_backquote($field_name[$j]) . ', ';
119 } // end for
120 unset($unique_cnt);
121 $unique = preg_replace('@, $@', '', $unique);
122 if (strlen($unique)) {
123 $sql_query .= ', UNIQUE (' . $unique . ')';
124 $query_cpy .= ',' . "\n" . ' UNIQUE (' . $unique . ')';
126 unset($unique);
128 // Builds the FULLTEXT statements
129 $fulltext = '';
130 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
131 for ($i = 0; $i < $fulltext_cnt; $i++) {
132 $j = $field_fulltext[$i];
133 if (isset($field_name[$j]) && strlen($field_name[$j])) {
134 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
136 } // end for
138 $fulltext = preg_replace('@, $@', '', $fulltext);
139 if (strlen($fulltext)) {
140 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
141 $query_cpy .= ',' . "\n" . ' FULLTEXT (' . $fulltext . ')';
143 unset($fulltext);
145 // Builds the 'create table' statement
146 $sql_query = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $sql_query . ')';
147 $query_cpy = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $query_cpy . "\n" . ')';
149 // Adds table type, character set and comments
150 if (!empty($tbl_type) && ($tbl_type != 'Default')) {
151 $sql_query .= ' ' . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type;
152 $query_cpy .= "\n" . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type;
154 if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) {
155 $sql_query .= PMA_generateCharsetQueryPart($tbl_collation);
156 $query_cpy .= "\n" . PMA_generateCharsetQueryPart($tbl_collation);
159 if (!empty($comment)) {
160 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
161 $query_cpy .= "\n" . 'COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
164 // Executes the query
165 $error_create = false;
166 $result = PMA_DBI_try_query($sql_query) or $error_create = true;
168 if ($error_create == false) {
169 $sql_query = $query_cpy . ';';
170 unset($query_cpy);
171 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenCreated;
173 // garvin: If comments were sent, enable relation stuff
174 require_once './libraries/relation.lib.php';
175 require_once './libraries/transformations.lib.php';
177 $cfgRelation = PMA_getRelationsParam();
179 // garvin: Update comment table, if a comment was set.
180 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
181 foreach ($field_comments AS $fieldindex => $fieldcomment) {
182 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
183 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
188 // garvin: Update comment table for mime types [MIME]
189 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
190 foreach ($field_mimetype AS $fieldindex => $mimetype) {
191 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
192 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
197 require './' . $cfg['DefaultTabTable'];
198 $abort = true;
199 exit();
200 } else {
201 PMA_mysqlDie('', '', '', $err_url, false);
202 // garvin: An error happened while inserting/updating a table definition.
203 // to prevent total loss of that data, we embed the form once again.
204 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
205 $num_fields = $orig_num_fields;
206 $regenerate = true;
208 } // end do create table
211 * Displays the form used to define the structure of the table
213 if ($abort == false) {
214 if (isset($num_fields)) {
215 $num_fields = intval($num_fields);
217 // No table name
218 if (!isset($table) || trim($table) == '') {
219 PMA_mysqlDie($strTableEmpty, '', '', $err_url);
221 // No valid number of fields
222 elseif (empty($num_fields) || !is_int($num_fields)) {
223 PMA_mysqlDie($strFieldsEmpty, '', '', $err_url);
225 // Does table exist?
226 elseif (!(PMA_DBI_get_fields($db, $table) === false)) {
227 PMA_mysqlDie(sprintf($strTableAlreadyExists, htmlspecialchars($table)), '', '', $err_url);
229 // Table name and number of fields are valid -> show the form
230 else {
231 $action = 'tbl_create.php';
232 require './libraries/tbl_properties.inc.php';
233 // Displays the footer
234 require_once './libraries/footer.inc.php';