Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / tbl_create.php
blob63cd26a1dae31d7bfd1bd4706a7bfa18256dcd57
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package PhpMyAdmin
5 */
7 /**
8 * Get some core libraries
9 */
10 require_once './libraries/common.inc.php';
12 $action = 'tbl_create.php';
14 require_once './libraries/header.inc.php';
15 $titles = PMA_buildActionTitles();
17 // Check parameters
18 PMA_checkParameters(array('db'));
20 /* Check if database name is empty */
21 if (strlen($db) == 0) {
22 PMA_mysqlDie(__('The database name is empty!'), '', '', 'main.php');
25 /**
26 * Defines the url to return to in case of error in a sql statement
28 if (PMA_DBI_get_columns($db, $table)) {
29 // table exists already
30 PMA_mysqlDie(
31 sprintf(__('Table %s already exists!'), htmlspecialchars($table)),
32 '',
33 '',
34 'db_structure.php?' . PMA_generate_common_url($db)
38 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
40 // check number of fields to be created
41 if (isset($_REQUEST['submit_num_fields'])) {
42 $regenerate = true; // for libraries/tbl_properties.inc.php
43 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
44 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
45 $num_fields = (int) $_REQUEST['num_fields'];
46 } else {
47 $num_fields = 4;
50 /**
51 * Selects the database to work with
53 if (!PMA_DBI_select_db($db)) {
54 PMA_mysqlDie(
55 sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
56 '',
57 '',
58 'main.php'
62 /**
63 * The form used to define the structure of the table has been submitted
65 if (isset($_REQUEST['do_save_data'])) {
66 $sql_query = '';
68 // Transforms the radio button field_key into 3 arrays
69 $field_cnt = count($_REQUEST['field_name']);
70 for ($i = 0; $i < $field_cnt; ++$i) {
71 if (isset($_REQUEST['field_key'][$i])) {
72 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
73 $field_primary[] = $i;
75 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
76 $field_index[] = $i;
78 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
79 $field_unique[] = $i;
81 } // end if
82 } // end for
84 // Builds the fields creation statements
85 for ($i = 0; $i < $field_cnt; $i++) {
86 // '0' is also empty for php :-(
87 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
88 continue;
91 $query = PMA_Table::generateFieldSpec(
92 $_REQUEST['field_name'][$i],
93 $_REQUEST['field_type'][$i],
94 $_REQUEST['field_length'][$i],
95 $_REQUEST['field_attribute'][$i],
96 isset($_REQUEST['field_collation'][$i])
97 ? $_REQUEST['field_collation'][$i]
98 : '',
99 isset($_REQUEST['field_null'][$i])
100 ? $_REQUEST['field_null'][$i]
101 : 'NOT NULL',
102 $_REQUEST['field_default_type'][$i],
103 $_REQUEST['field_default_value'][$i],
104 isset($_REQUEST['field_extra'][$i])
105 ? $_REQUEST['field_extra'][$i]
106 : false,
107 isset($_REQUEST['field_comments'][$i])
108 ? $_REQUEST['field_comments'][$i]
109 : '',
110 $field_primary,
114 $query .= ', ';
115 $sql_query .= $query;
116 } // end for
117 unset($field_cnt, $query);
118 $sql_query = preg_replace('@, $@', '', $sql_query);
120 // Builds the primary keys statements
121 $primary = '';
122 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
123 for ($i = 0; $i < $primary_cnt; $i++) {
124 $j = $field_primary[$i];
125 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
126 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
128 } // end for
129 unset($primary_cnt);
130 $primary = preg_replace('@, $@', '', $primary);
131 if (strlen($primary)) {
132 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
134 unset($primary);
136 // Builds the indexes statements
137 $index = '';
138 $index_cnt = (isset($field_index) ? count($field_index) : 0);
139 for ($i = 0;$i < $index_cnt; $i++) {
140 $j = $field_index[$i];
141 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
142 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
144 } // end for
145 unset($index_cnt);
146 $index = preg_replace('@, $@', '', $index);
147 if (strlen($index)) {
148 $sql_query .= ', INDEX (' . $index . ')';
150 unset($index);
152 // Builds the uniques statements
153 $unique = '';
154 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
155 for ($i = 0; $i < $unique_cnt; $i++) {
156 $j = $field_unique[$i];
157 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
158 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
160 } // end for
161 unset($unique_cnt);
162 $unique = preg_replace('@, $@', '', $unique);
163 if (strlen($unique)) {
164 $sql_query .= ', UNIQUE (' . $unique . ')';
166 unset($unique);
168 // Builds the FULLTEXT statements
169 $fulltext = '';
170 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
171 for ($i = 0; $i < $fulltext_cnt; $i++) {
172 $j = $field_fulltext[$i];
173 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
174 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
176 } // end for
178 $fulltext = preg_replace('@, $@', '', $fulltext);
179 if (strlen($fulltext)) {
180 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
182 unset($fulltext);
184 // Builds the 'create table' statement
185 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
186 . ' (' . $sql_query . ')';
188 // Adds table type, character set, comments and partition definition
189 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
190 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
192 if (!empty($_REQUEST['tbl_collation'])) {
193 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
195 if (!empty($_REQUEST['comment'])) {
196 $sql_query .= ' COMMENT = \'' . PMA_sqlAddSlashes($_REQUEST['comment']) . '\'';
198 if (!empty($_REQUEST['partition_definition'])) {
199 $sql_query .= ' ' . PMA_sqlAddSlashes($_REQUEST['partition_definition']);
201 $sql_query .= ';';
203 // Executes the query
204 $result = PMA_DBI_try_query($sql_query);
206 if ($result) {
208 // If comments were sent, enable relation stuff
209 include_once './libraries/transformations.lib.php';
211 // Update comment table for mime types [MIME]
212 if (isset($_REQUEST['field_mimetype'])
213 && is_array($_REQUEST['field_mimetype'])
214 && $cfg['BrowseMIME']) {
215 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
216 if (isset($_REQUEST['field_name'][$fieldindex])
217 && strlen($_REQUEST['field_name'][$fieldindex])) {
218 PMA_setMIME(
219 $db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
220 $_REQUEST['field_transformation'][$fieldindex],
221 $_REQUEST['field_transformation_options'][$fieldindex]
227 $message = PMA_Message::success(__('Table %1$s has been created.'));
228 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
230 if ($GLOBALS['is_ajax_request'] == true) {
233 * construct the html for the newly created table's row to be appended
234 * to the list of tables.
236 * Logic taken from db_structure.php
239 $tbl_url_params = array();
240 $tbl_url_params['db'] = $db;
241 $tbl_url_params['table'] = $table;
242 $is_show_stats = $cfg['ShowStats'];
244 $tbl_stats_result = PMA_DBI_query('SHOW TABLE STATUS FROM '
245 . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddSlashes($table, true) . '\';');
246 $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
247 PMA_DBI_free_result($tbl_stats_result);
248 unset($tbl_stats_result);
250 if ($is_show_stats) {
251 $sum_size = (double) 0;
252 $overhead_size = (double) 0;
253 $overhead_check = '';
255 $tblsize = doubleval($tbl_stats['Data_length']) + doubleval($tbl_stats['Index_length']);
256 $sum_size += $tblsize;
257 list($formatted_size, $unit) = PMA_formatByteDown($tblsize, 3, ($tblsize > 0) ? 1 : 0);
258 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
259 list($formatted_overhead, $overhead_unit) = PMA_formatByteDown($tbl_stats['Data_free'], 3, ($tbl_stats['Data_free'] > 0) ? 1 : 0);
260 $overhead_size += $tbl_stats['Data_free'];
263 if (isset($formatted_overhead)) {
264 $overhead = '<span>' . $formatted_overhead . '</span> <span class="unit">' . $overhead_unit . '</span>';
265 unset($formatted_overhead);
266 } else {
267 $overhead = '-';
271 $new_table_string = '<tr>' . "\n";
272 $new_table_string .= '<td align="center"> <input type="checkbox" id="checkbox_tbl_" name="selected_tbl[]" value="'.htmlspecialchars($table).'" /> </td>' . "\n";
274 $new_table_string .= '<th>';
275 $new_table_string .= '<a href="sql.php' . PMA_generate_common_url($tbl_url_params) . '">'. $table . '</a>';
277 if (PMA_Tracker::isActive()) {
278 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
279 if (PMA_Tracker::isTracked($db, $truename)) {
280 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '">';
281 $new_table_string .= PMA_getImage('eye.png', __('Tracking is active.'));
282 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
283 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '">';
284 $new_table_string .= PMA_getImage('eye_grey.png', __('Tracking is not active.'));
286 unset($truename);
288 $new_table_string .= '</th>' . "\n";
290 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
292 $new_table_string .= '<td><a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Structure'] . '</a></td>' . "\n";
294 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
296 $new_table_string .= '<td><a href="tbl_change.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Insert'] . '</a></td>' . "\n";
298 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
300 $new_table_string .= '<td><a class="drop_table_anchor" href="sql.php' . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query=';
301 $new_table_string .= urlencode('DROP TABLE ' . PMA_backquote($table));
302 $new_table_string .= '">';
303 $new_table_string .= $titles['Drop'];
304 $new_table_string .= '</a></td>' . "\n";
306 $new_table_string .= '<td class="value">' . $tbl_stats['Rows'] . '</td>' . "\n";
308 $new_table_string .= '<td nowrap="nowrap">' . $tbl_stats['Engine'] . '</td>' . "\n";
310 $new_table_string .= '<td> <dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'. $tbl_stats['Collation'] .'</dfn></td>' . "\n";
312 if ($is_show_stats) {
313 $new_table_string .= '<td class="value tbl_size"> <a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '#showusage" ><span>' . $formatted_size . '</span> <span class="unit">' . $unit . '</class></a> </td>' . "\n" ;
314 $new_table_string .= '<td class="value tbl_overhead">' . $overhead . '</td>' . "\n" ;
317 $new_table_string .= '</tr>' . "\n";
319 $extra_data['new_table_string'] = $new_table_string;
321 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
324 $display_query = $sql_query;
325 $sql_query = '';
327 // read table info on this newly created table, in case
328 // the next page is Structure
329 $reread_info = true;
330 include './libraries/tbl_info.inc.php';
332 // do not switch to sql.php - as there is no row to be displayed on a new table
333 if ($cfg['DefaultTabTable'] === 'sql.php') {
334 include './tbl_structure.php';
335 } else {
336 include './' . $cfg['DefaultTabTable'];
338 exit;
339 } else {
340 if ($GLOBALS['is_ajax_request'] == true) {
341 PMA_ajaxResponse(PMA_DBI_getError(), false);
342 } else {
343 PMA_mysqlDie('', '', '', $err_url, false);
344 // An error happened while inserting/updating a table definition.
345 // to prevent total loss of that data, we embed the form once again.
346 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
347 $num_fields = $_REQUEST['orig_num_fields'];
348 $regenerate = true;
351 } // end do create table
354 * Displays the form used to define the structure of the table
357 // This div is used to show the content(eg: create table form with more columns) fetched with AJAX subsequently.
358 if ($GLOBALS['is_ajax_request'] != true) {
359 echo('<div id="create_table_div">');
362 require './libraries/tbl_properties.inc.php';
363 // Displays the footer
364 require './libraries/footer.inc.php';
366 if ($GLOBALS['is_ajax_request'] != true) {
367 echo('</div>');