Translated using Weblate (Spanish)
[phpmyadmin.git] / tbl_create.php
blob510e16a1918fc0c93934745bbf6692707eeb7ca5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays table create form and handles it
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get some core libraries
12 require_once 'libraries/common.inc.php';
14 $action = 'tbl_create.php';
16 $titles = PMA_Util::buildActionTitles();
18 // Check parameters
19 PMA_Util::checkParameters(array('db'));
21 /* Check if database name is empty */
22 if (strlen($db) == 0) {
23 PMA_Util::mysqlDie(
24 __('The database name is empty!'), '', '', 'index.php'
28 /**
29 * Defines the url to return to in case of error in a sql statement
31 if (PMA_DBI_getColumns($db, $table)) {
32 // table exists already
33 PMA_Util::mysqlDie(
34 sprintf(__('Table %s already exists!'), htmlspecialchars($table)),
35 '',
36 '',
37 'db_structure.php?' . PMA_generate_common_url($db)
41 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
43 // check number of fields to be created
44 if (isset($_REQUEST['submit_num_fields'])) {
45 $regenerate = true; // for libraries/tbl_columns_definition_form.inc.php
46 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
47 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
48 $num_fields = (int) $_REQUEST['num_fields'];
49 } else {
50 $num_fields = 4;
53 /**
54 * Selects the database to work with
56 if (!PMA_DBI_selectDb($db)) {
57 PMA_Util::mysqlDie(
58 sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
59 '',
60 '',
61 'index.php'
65 /**
66 * The form used to define the structure of the table has been submitted
68 if (isset($_REQUEST['do_save_data'])) {
69 $sql_query = '';
71 // Transforms the radio button field_key into 3 arrays
72 $field_cnt = count($_REQUEST['field_name']);
73 for ($i = 0; $i < $field_cnt; ++$i) {
74 if (isset($_REQUEST['field_key'][$i])) {
75 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
76 $field_primary[] = $i;
78 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
79 $field_index[] = $i;
81 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
82 $field_unique[] = $i;
84 } // end if
85 } // end for
87 // Builds the fields creation statements
88 for ($i = 0; $i < $field_cnt; $i++) {
89 // '0' is also empty for php :-(
90 if (empty($_REQUEST['field_name'][$i])
91 && $_REQUEST['field_name'][$i] != '0'
92 ) {
93 continue;
96 $query = PMA_Table::generateFieldSpec(
97 $_REQUEST['field_name'][$i],
98 $_REQUEST['field_type'][$i],
99 $i,
100 $_REQUEST['field_length'][$i],
101 $_REQUEST['field_attribute'][$i],
102 isset($_REQUEST['field_collation'][$i])
103 ? $_REQUEST['field_collation'][$i]
104 : '',
105 isset($_REQUEST['field_null'][$i])
106 ? $_REQUEST['field_null'][$i]
107 : 'NOT NULL',
108 $_REQUEST['field_default_type'][$i],
109 $_REQUEST['field_default_value'][$i],
110 isset($_REQUEST['field_extra'][$i])
111 ? $_REQUEST['field_extra'][$i]
112 : false,
113 isset($_REQUEST['field_comments'][$i])
114 ? $_REQUEST['field_comments'][$i]
115 : '',
116 $field_primary,
120 $query .= ', ';
121 $sql_query .= $query;
122 } // end for
123 unset($field_cnt, $query);
124 $sql_query = preg_replace('@, $@', '', $sql_query);
126 // Builds the primary keys statements
127 $primary = '';
128 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
129 for ($i = 0; $i < $primary_cnt; $i++) {
130 $j = $field_primary[$i];
131 if (isset($_REQUEST['field_name'][$j])
132 && strlen($_REQUEST['field_name'][$j])
134 $primary .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
136 } // end for
137 unset($primary_cnt);
138 $primary = preg_replace('@, $@', '', $primary);
139 if (strlen($primary)) {
140 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
142 unset($primary);
144 // Builds the indexes statements
145 $index = '';
146 $index_cnt = (isset($field_index) ? count($field_index) : 0);
147 for ($i = 0;$i < $index_cnt; $i++) {
148 $j = $field_index[$i];
149 if (isset($_REQUEST['field_name'][$j])
150 && strlen($_REQUEST['field_name'][$j])
152 $index .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
154 } // end for
155 unset($index_cnt);
156 $index = preg_replace('@, $@', '', $index);
157 if (strlen($index)) {
158 $sql_query .= ', INDEX (' . $index . ')';
160 unset($index);
162 // Builds the uniques statements
163 $unique = '';
164 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
165 for ($i = 0; $i < $unique_cnt; $i++) {
166 $j = $field_unique[$i];
167 if (isset($_REQUEST['field_name'][$j])
168 && strlen($_REQUEST['field_name'][$j])
170 $unique .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
172 } // end for
173 unset($unique_cnt);
174 $unique = preg_replace('@, $@', '', $unique);
175 if (strlen($unique)) {
176 $sql_query .= ', UNIQUE (' . $unique . ')';
178 unset($unique);
180 // Builds the FULLTEXT statements
181 $fulltext = '';
182 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
183 for ($i = 0; $i < $fulltext_cnt; $i++) {
184 $j = $field_fulltext[$i];
185 if (isset($_REQUEST['field_name'][$j])
186 && strlen($_REQUEST['field_name'][$j])
188 $fulltext .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
190 } // end for
192 $fulltext = preg_replace('@, $@', '', $fulltext);
193 if (strlen($fulltext)) {
194 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
196 unset($fulltext);
198 // Builds the 'create table' statement
199 $sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.'
200 . PMA_Util::backquote($table) . ' (' . $sql_query . ')';
202 // Adds table type, character set, comments and partition definition
203 if (!empty($_REQUEST['tbl_storage_engine'])
204 && ($_REQUEST['tbl_storage_engine'] != 'Default')
206 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
208 if (!empty($_REQUEST['tbl_collation'])) {
209 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
211 if (!empty($_REQUEST['comment'])) {
212 $sql_query .= ' COMMENT = \''
213 . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
215 if (!empty($_REQUEST['partition_definition'])) {
216 $sql_query .= ' ' . PMA_Util::sqlAddSlashes(
217 $_REQUEST['partition_definition']
220 $sql_query .= ';';
222 // Executes the query
223 $result = PMA_DBI_tryQuery($sql_query);
225 if ($result) {
227 // If comments were sent, enable relation stuff
228 include_once 'libraries/transformations.lib.php';
230 // Update comment table for mime types [MIME]
231 if (isset($_REQUEST['field_mimetype'])
232 && is_array($_REQUEST['field_mimetype'])
233 && $cfg['BrowseMIME']
235 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
236 if (isset($_REQUEST['field_name'][$fieldindex])
237 && strlen($_REQUEST['field_name'][$fieldindex])
239 PMA_setMIME(
240 $db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
241 $_REQUEST['field_transformation'][$fieldindex],
242 $_REQUEST['field_transformation_options'][$fieldindex]
248 $message = PMA_Message::success(__('Table %1$s has been created.'));
249 $message->addParam(
250 PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table)
253 if ($GLOBALS['is_ajax_request'] == true) {
256 * construct the html for the newly created table's row to be appended
257 * to the list of tables.
259 * Logic taken from db_structure.php
262 $tbl_url_params = array();
263 $tbl_url_params['db'] = $db;
264 $tbl_url_params['table'] = $table;
265 $is_show_stats = $cfg['ShowStats'];
267 $tbl_stats_result = PMA_DBI_query(
268 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db)
269 . ' LIKE \'' . PMA_Util::sqlAddSlashes($table, true) . '\';'
271 $tbl_stats = PMA_DBI_fetchAssoc($tbl_stats_result);
272 PMA_DBI_freeResult($tbl_stats_result);
273 unset($tbl_stats_result);
275 if ($is_show_stats) {
276 $sum_size = (double) 0;
277 $overhead_size = (double) 0;
278 $overhead_check = '';
280 $tblsize = doubleval($tbl_stats['Data_length'])
281 + doubleval($tbl_stats['Index_length']);
282 $sum_size += $tblsize;
283 list($formatted_size, $unit) = PMA_Util::formatByteDown(
284 $tblsize,
286 ($tblsize > 0) ? 1 : 0
288 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
289 list($formatted_overhead, $overhead_unit)
290 = PMA_Util::formatByteDown(
291 $tbl_stats['Data_free'],
293 ($tbl_stats['Data_free'] > 0) ? 1 : 0
295 $overhead_size += $tbl_stats['Data_free'];
298 if (isset($formatted_overhead)) {
299 $overhead = '<span>' . $formatted_overhead . '</span>'
300 . '<span class="unit">' . $overhead_unit . '</span>';
301 unset($formatted_overhead);
302 } else {
303 $overhead = '-';
307 $new_table_string = '<tr>' . "\n";
308 $new_table_string .= '<td class="center">'
309 . '<input type="checkbox" id="checkbox_tbl_"'
310 . ' name="selected_tbl[]" value="'.htmlspecialchars($table).'" />'
311 . '</td>' . "\n";
313 $new_table_string .= '<th>';
314 $new_table_string .= '<a href="sql.php'
315 . PMA_generate_common_url($tbl_url_params) . '">'
316 . htmlspecialchars($table) . '</a>';
318 if (PMA_Tracker::isActive()) {
319 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
320 if (PMA_Tracker::isTracked($db, $truename)) {
321 $new_table_string .= '<a href="tbl_tracking.php'
322 . PMA_generate_common_url($tbl_url_params) . '">';
323 $new_table_string .= PMA_Util::getImage(
324 'eye.png', __('Tracking is active.')
326 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
327 $new_table_string .= '<a href="tbl_tracking.php'
328 . PMA_generate_common_url($tbl_url_params) . '">';
329 $new_table_string .= PMA_Util::getImage(
330 'eye_grey.png', __('Tracking is not active.')
333 unset($truename);
335 $new_table_string .= '</th>' . "\n";
337 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
339 $new_table_string .= '<td>'
340 . '<a href="tbl_structure.php'
341 . PMA_generate_common_url($tbl_url_params) . '">'
342 . $titles['Structure']
343 . '</a>'
344 . '</td>' . "\n";
346 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
348 $new_table_string .= '<td>'
349 . '<a href="tbl_change.php'
350 . PMA_generate_common_url($tbl_url_params) . '">'
351 . $titles['Insert']
352 . '</a>'
353 . '</td>' . "\n";
355 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
357 $new_table_string .= '<td>'
358 . '<a class="drop_table_anchor" href="sql.php'
359 . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query='
360 . urlencode('DROP TABLE ' . PMA_Util::backquote($table)) . '">'
361 . $titles['Drop']
362 . '</a>'
363 . '</td>' . "\n";
365 $new_table_string .= '<td class="value">'
366 . $tbl_stats['Rows']
367 . '</td>' . "\n";
369 $new_table_string .= '<td class="nowrap">'
370 . $tbl_stats['Engine']
371 . '</td>' . "\n";
373 $new_table_string .= '<td>'
374 . '<dfn title="'
375 . PMA_getCollationDescr($tbl_stats['Collation']) . '">'
376 . $tbl_stats['Collation']
377 .'</dfn>'
378 . '</td>' . "\n";
380 if ($is_show_stats) {
381 $new_table_string .= '<td class="value tbl_size">'
382 . '<a href="tbl_structure.php'
383 . PMA_generate_common_url($tbl_url_params) . '#showusage" >'
384 . '<span>' . $formatted_size . '</span>'
385 . '<span class="unit">' . $unit . '</span>'
386 . '</a>'
387 . '</td>' . "\n" ;
389 $new_table_string .= '<td class="value tbl_overhead">'
390 . $overhead
391 . '</td>' . "\n" ;
393 $new_table_string .= '</tr>' . "\n";
395 $formatted_sql = PMA_Util::getMessage(
396 $message, $sql_query, 'success'
399 $response = PMA_Response::getInstance();
400 $response->addJSON('message', $message);
401 $response->addJSON('formatted_sql', $formatted_sql);
402 $response->addJSON('new_table_string', $new_table_string);
403 } else {
405 $display_query = $sql_query;
406 $sql_query = '';
408 // read table info on this newly created table, in case
409 // the next page is Structure
410 $reread_info = true;
411 include 'libraries/tbl_info.inc.php';
413 // do not switch to sql.php
414 // as there is no row to be displayed on a new table
415 if ($cfg['DefaultTabTable'] === 'sql.php') {
416 include 'tbl_structure.php';
417 } else {
418 include '' . $cfg['DefaultTabTable'];
421 } else {
422 if ($GLOBALS['is_ajax_request'] == true) {
423 $response = PMA_Response::getInstance();
424 $response->isSuccess(false);
425 $response->addJSON('message', PMA_DBI_getError());
426 } else {
427 echo PMA_Util::mysqlDie('', '', '', $err_url, false);
428 // An error happened while inserting/updating a table definition.
429 // To prevent total loss of that data, we embed the form once again.
430 // The variable $regenerate will be used to restore data in
431 // libraries/tbl_columns_definition_form.inc.php
432 $num_fields = $_REQUEST['orig_num_fields'];
433 $regenerate = true;
436 exit;
437 } // end do create table
440 * Displays the form used to define the structure of the table
443 // This div is used to show the content(eg: create table form with more columns)
444 // fetched with AJAX subsequently.
445 if ($GLOBALS['is_ajax_request'] != true) {
446 echo('<div id="create_table_div">');
449 require 'libraries/tbl_columns_definition_form.inc.php';
451 if ($GLOBALS['is_ajax_request'] != true) {
452 echo('</div>');