bug #3383572 [interface] Cannot execute saved query
[phpmyadmin/madhuracj.git] / tbl_create.php
blob4d3171ad993c913a0219511387c8a6e82dbe6a27
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';
41 $titles = PMA_buildActionTitles();
43 // Check parameters
44 PMA_checkParameters(array('db'));
46 /* Check if database name is empty */
47 if (strlen($db) == 0) {
48 PMA_mysqlDie(__('The database name is empty!'), '', '', 'main.php');
51 /**
52 * Defines the url to return to in case of error in a sql statement
54 if (PMA_DBI_get_columns($db, $table)) {
55 // table exists already
56 PMA_mysqlDie(sprintf(__('Table %s already exists!'), htmlspecialchars($table)), '',
57 '', 'db_structure.php?' . PMA_generate_common_url($db));
60 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
62 // check number of fields to be created
63 if (isset($_REQUEST['submit_num_fields'])) {
64 $regenerate = true; // for libraries/tbl_properties.inc.php
65 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
66 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
67 $num_fields = (int) $_REQUEST['num_fields'];
68 } else {
69 $num_fields = 2;
72 /**
73 * Selects the database to work with
75 if (!PMA_DBI_select_db($db)) {
76 PMA_mysqlDie(sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
77 '', '', 'main.php');
80 /**
81 * The form used to define the structure of the table has been submitted
83 if (isset($_REQUEST['do_save_data'])) {
84 $sql_query = '';
86 // Transforms the radio button field_key into 3 arrays
87 $field_cnt = count($_REQUEST['field_name']);
88 for ($i = 0; $i < $field_cnt; ++$i) {
89 if (isset($_REQUEST['field_key'][$i])) {
90 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
91 $field_primary[] = $i;
93 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
94 $field_index[] = $i;
96 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
97 $field_unique[] = $i;
99 } // end if
100 } // end for
102 // Builds the fields creation statements
103 for ($i = 0; $i < $field_cnt; $i++) {
104 // '0' is also empty for php :-(
105 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
106 continue;
109 $query = PMA_Table::generateFieldSpec(
110 $_REQUEST['field_name'][$i],
111 $_REQUEST['field_type'][$i],
112 $_REQUEST['field_length'][$i],
113 $_REQUEST['field_attribute'][$i],
114 isset($_REQUEST['field_collation'][$i])
115 ? $_REQUEST['field_collation'][$i]
116 : '',
117 isset($_REQUEST['field_null'][$i])
118 ? $_REQUEST['field_null'][$i]
119 : 'NOT NULL',
120 $_REQUEST['field_default_type'][$i],
121 $_REQUEST['field_default_value'][$i],
122 isset($_REQUEST['field_extra'][$i])
123 ? $_REQUEST['field_extra'][$i]
124 : false,
125 isset($_REQUEST['field_comments'][$i])
126 ? $_REQUEST['field_comments'][$i]
127 : '',
128 $field_primary,
129 $i);
131 $query .= ', ';
132 $sql_query .= $query;
133 } // end for
134 unset($field_cnt, $query);
135 $sql_query = preg_replace('@, $@', '', $sql_query);
137 // Builds the primary keys statements
138 $primary = '';
139 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
140 for ($i = 0; $i < $primary_cnt; $i++) {
141 $j = $field_primary[$i];
142 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
143 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
145 } // end for
146 unset($primary_cnt);
147 $primary = preg_replace('@, $@', '', $primary);
148 if (strlen($primary)) {
149 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
151 unset($primary);
153 // Builds the indexes statements
154 $index = '';
155 $index_cnt = (isset($field_index) ? count($field_index) : 0);
156 for ($i = 0;$i < $index_cnt; $i++) {
157 $j = $field_index[$i];
158 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
159 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
161 } // end for
162 unset($index_cnt);
163 $index = preg_replace('@, $@', '', $index);
164 if (strlen($index)) {
165 $sql_query .= ', INDEX (' . $index . ')';
167 unset($index);
169 // Builds the uniques statements
170 $unique = '';
171 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
172 for ($i = 0; $i < $unique_cnt; $i++) {
173 $j = $field_unique[$i];
174 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
175 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
177 } // end for
178 unset($unique_cnt);
179 $unique = preg_replace('@, $@', '', $unique);
180 if (strlen($unique)) {
181 $sql_query .= ', UNIQUE (' . $unique . ')';
183 unset($unique);
185 // Builds the FULLTEXT statements
186 $fulltext = '';
187 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
188 for ($i = 0; $i < $fulltext_cnt; $i++) {
189 $j = $field_fulltext[$i];
190 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
191 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
193 } // end for
195 $fulltext = preg_replace('@, $@', '', $fulltext);
196 if (strlen($fulltext)) {
197 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
199 unset($fulltext);
201 // Builds the 'create table' statement
202 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
203 . ' (' . $sql_query . ')';
205 // Adds table type, character set, comments and partition definition
206 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
207 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
209 if (!empty($_REQUEST['tbl_collation'])) {
210 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
212 if (!empty($_REQUEST['comment'])) {
213 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\'';
215 if (!empty($_REQUEST['partition_definition'])) {
216 $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']);
218 $sql_query .= ';';
220 // Executes the query
221 $result = PMA_DBI_try_query($sql_query);
223 if ($result) {
225 // If comments were sent, enable relation stuff
226 require_once './libraries/transformations.lib.php';
228 // Update comment table for mime types [MIME]
229 if (isset($_REQUEST['field_mimetype'])
230 && is_array($_REQUEST['field_mimetype'])
231 && $cfg['BrowseMIME']) {
232 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
233 if (isset($_REQUEST['field_name'][$fieldindex])
234 && strlen($_REQUEST['field_name'][$fieldindex])) {
235 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
236 $_REQUEST['field_transformation'][$fieldindex],
237 $_REQUEST['field_transformation_options'][$fieldindex]);
242 $message = PMA_Message::success(__('Table %1$s has been created.'));
243 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
245 if($GLOBALS['is_ajax_request'] == true) {
248 * construct the html for the newly created table's row to be appended
249 * to the list of tables.
251 * Logic taken from db_structure.php
254 $tbl_url_params = array();
255 $tbl_url_params['db'] = $db;
256 $tbl_url_params['table'] = $table;
257 $is_show_stats = $cfg['ShowStats'];
259 $tbl_stats_result = PMA_DBI_query('SHOW TABLE STATUS FROM '
260 . PMA_backquote($db) . ' LIKE \'' . addslashes($table) . '\';');
261 $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
262 PMA_DBI_free_result($tbl_stats_result);
263 unset($tbl_stats_result);
265 if ($is_show_stats) {
266 $sum_size = (double) 0;
267 $overhead_size = (double) 0;
268 $overhead_check = '';
270 $tblsize = doubleval($tbl_stats['Data_length']) + doubleval($tbl_stats['Index_length']);
271 $sum_size += $tblsize;
272 list($formatted_size, $unit) = PMA_formatByteDown($tblsize, 3, ($tblsize > 0) ? 1 : 0);
273 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
274 list($formatted_overhead, $overhead_unit) = PMA_formatByteDown($tbl_stats['Data_free'], 3, ($tbl_stats['Data_free'] > 0) ? 1 : 0);
275 $overhead_size += $tbl_stats['Data_free'];
278 if (isset($formatted_overhead)) {
279 $overhead = $formatted_overhead . ' ' . $overhead_unit;
280 unset($formatted_overhead);
281 } else {
282 $overhead = '-';
286 $new_table_string = '<tr>' . "\n";
287 $new_table_string .= '<td align="center"> <input type="checkbox" id="checkbox_tbl_" name="selected_tbl[]" value="'.htmlspecialchars($table).'" /> </td>' . "\n";
289 $new_table_string .= '<th>';
290 $new_table_string .= '<a href="sql.php' . PMA_generate_common_url($tbl_url_params) . '">'. $table . '</a>';
292 if (PMA_Tracker::isActive()) {
293 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
294 if (PMA_Tracker::isTracked($db, $truename)) {
295 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '"><img class="icon" width="14" height="14" src="' . $pmaThemeImage . 'eye.png" alt="' . __('Tracking is active.') . '" title="' . __('Tracking is active.') . '" /></a>';
296 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
297 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '"><img class="icon" width="14" height="14" src="' . $pmaThemeImage . 'eye_grey.png" alt="' . __('Tracking is not active.') . '" title="' . __('Tracking is not active.') . '" /></a>';
299 unset($truename);
301 $new_table_string .= '</th>' . "\n";
303 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
305 $new_table_string .= '<td><a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Structure'] . '</a></td>' . "\n";
307 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
309 $new_table_string .= '<td><a href="tbl_change.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Insert'] . '</a></td>' . "\n";
311 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
313 $new_table_string .= '<td><a class="drop_table_anchor" href="sql.php' . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query=';
314 $new_table_string .= urlencode('DROP TABLE ' . PMA_backquote($table));
315 $new_table_string .= '">';
316 $new_table_string .= $titles['Drop'];
317 $new_table_string .= '</a></td>' . "\n";
319 $new_table_string .= '<td class="value">' . $tbl_stats['Rows'] . '</td>' . "\n";
321 $new_table_string .= '<td nowrap="nowrap">' . $tbl_stats['Engine'] . '</td>' . "\n";
323 $new_table_string .= '<td> <dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'. $tbl_stats['Collation'] .'</dfn></td>' . "\n";
325 if($is_show_stats) {
326 $new_table_string .= '<td class="value"> <a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '#showusage" >' . $formatted_size . ' ' . $unit . '</a> </td>' . "\n" ;
327 $new_table_string .= '<td class="value">' . $overhead . '</td>' . "\n" ;
330 $new_table_string .= '</tr>' . "\n";
332 $extra_data['new_table_string'] = $new_table_string;
334 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
337 $display_query = $sql_query;
338 $sql_query = '';
340 // read table info on this newly created table, in case
341 // the next page is Structure
342 $reread_info = true;
343 require './libraries/tbl_info.inc.php';
345 // do not switch to sql.php - as there is no row to be displayed on a new table
346 if ($cfg['DefaultTabTable'] === 'sql.php') {
347 require './tbl_structure.php';
348 } else {
349 require './' . $cfg['DefaultTabTable'];
351 exit;
352 } else {
353 if ($GLOBALS['is_ajax_request'] == true) {
354 PMA_ajaxResponse(PMA_DBI_getError(), false);
355 } else {
356 PMA_mysqlDie('', '', '', $err_url, false);
357 // An error happened while inserting/updating a table definition.
358 // to prevent total loss of that data, we embed the form once again.
359 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
360 $num_fields = $_REQUEST['orig_num_fields'];
361 $regenerate = true;
364 } // end do create table
367 * Displays the form used to define the structure of the table
370 // This div is used to show the content(eg: create table form with more columns) fetched with AJAX subsequently.
371 if($GLOBALS['is_ajax_request'] != true) {
372 echo('<div id="create_table_div">');
375 require './libraries/tbl_properties.inc.php';
376 // Displays the footer
377 require './libraries/footer.inc.php';
379 if($GLOBALS['is_ajax_request'] != true) {
380 echo('</div>');