Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / tbl_create.php
blobced10a6247fd14fbb5c2f951f537ae99d7f4ee2f
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 ($GLOBALS['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 (!$GLOBALS['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 include_once 'libraries/create_addfield.lib.php';
72 // get column addition statements
73 $sql_statement = PMA_getColumnCreationStatements(true);
75 // Builds the 'create table' statement
76 $sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.'
77 . PMA_Util::backquote($table) . ' (' . $sql_statement . ')';
79 // Adds table type, character set, comments and partition definition
80 if (!empty($_REQUEST['tbl_storage_engine'])
81 && ($_REQUEST['tbl_storage_engine'] != 'Default')
82 ) {
83 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
85 if (!empty($_REQUEST['tbl_collation'])) {
86 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
88 if (!empty($_REQUEST['comment'])) {
89 $sql_query .= ' COMMENT = \''
90 . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
92 if (!empty($_REQUEST['partition_definition'])) {
93 $sql_query .= ' ' . PMA_Util::sqlAddSlashes(
94 $_REQUEST['partition_definition']
97 $sql_query .= ';';
99 // Executes the query
100 $result = $GLOBALS['dbi']->tryQuery($sql_query);
102 if ($result) {
104 // If comments were sent, enable relation stuff
105 include_once 'libraries/transformations.lib.php';
107 // Update comment table for mime types [MIME]
108 if (isset($_REQUEST['field_mimetype'])
109 && is_array($_REQUEST['field_mimetype'])
110 && $cfg['BrowseMIME']
112 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
113 if (isset($_REQUEST['field_name'][$fieldindex])
114 && strlen($_REQUEST['field_name'][$fieldindex])
116 PMA_setMIME(
117 $db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
118 $_REQUEST['field_transformation'][$fieldindex],
119 $_REQUEST['field_transformation_options'][$fieldindex]
125 $message = PMA_Message::success(__('Table %1$s has been created.'));
126 $message->addParam(
127 PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table)
130 if ($GLOBALS['is_ajax_request'] == true) {
133 * construct the html for the newly created table's row to be appended
134 * to the list of tables.
136 * Logic taken from db_structure.php
139 $tbl_url_params = array();
140 $tbl_url_params['db'] = $db;
141 $tbl_url_params['table'] = $table;
142 $is_show_stats = $cfg['ShowStats'];
144 $tbl_stats_result = $GLOBALS['dbi']->query(
145 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db)
146 . ' LIKE \'' . PMA_Util::sqlAddSlashes($table, true) . '\';'
148 $tbl_stats = $GLOBALS['dbi']->fetchAssoc($tbl_stats_result);
149 $GLOBALS['dbi']->freeResult($tbl_stats_result);
150 unset($tbl_stats_result);
152 if ($is_show_stats) {
153 $sum_size = (double) 0;
154 $overhead_size = (double) 0;
155 $overhead_check = '';
157 $tblsize = doubleval($tbl_stats['Data_length'])
158 + doubleval($tbl_stats['Index_length']);
159 $sum_size += $tblsize;
160 list($formatted_size, $unit) = PMA_Util::formatByteDown(
161 $tblsize,
163 ($tblsize > 0) ? 1 : 0
165 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
166 list($formatted_overhead, $overhead_unit)
167 = PMA_Util::formatByteDown(
168 $tbl_stats['Data_free'],
170 ($tbl_stats['Data_free'] > 0) ? 1 : 0
172 $overhead_size += $tbl_stats['Data_free'];
175 if (isset($formatted_overhead)) {
176 $overhead = '<span>' . $formatted_overhead . '</span>'
177 . '<span class="unit">' . $overhead_unit . '</span>';
178 unset($formatted_overhead);
179 } else {
180 $overhead = '-';
184 $new_table_string = '<tr>' . "\n";
185 $new_table_string .= '<td class="center">'
186 . '<input type="checkbox" id="checkbox_tbl_"'
187 . ' name="selected_tbl[]" value="'.htmlspecialchars($table).'" />'
188 . '</td>' . "\n";
190 $new_table_string .= '<th>';
191 $new_table_string .= '<a href="sql.php'
192 . PMA_generate_common_url($tbl_url_params) . '">'
193 . htmlspecialchars($table) . '</a>';
195 if (PMA_Tracker::isActive()) {
196 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
197 if (PMA_Tracker::isTracked($db, $truename)) {
198 $new_table_string .= '<a href="tbl_tracking.php'
199 . PMA_generate_common_url($tbl_url_params) . '">';
200 $new_table_string .= PMA_Util::getImage(
201 'eye.png', __('Tracking is active.')
203 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
204 $new_table_string .= '<a href="tbl_tracking.php'
205 . PMA_generate_common_url($tbl_url_params) . '">';
206 $new_table_string .= PMA_Util::getImage(
207 'eye_grey.png', __('Tracking is not active.')
210 unset($truename);
212 $new_table_string .= '</th>' . "\n";
214 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
216 $new_table_string .= '<td>'
217 . '<a href="tbl_structure.php'
218 . PMA_generate_common_url($tbl_url_params) . '">'
219 . $titles['Structure']
220 . '</a>'
221 . '</td>' . "\n";
223 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
225 $new_table_string .= '<td>'
226 . '<a href="tbl_change.php'
227 . PMA_generate_common_url($tbl_url_params) . '">'
228 . $titles['Insert']
229 . '</a>'
230 . '</td>' . "\n";
232 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
234 $new_table_string .= '<td>'
235 . '<a class="drop_table_anchor" href="sql.php'
236 . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query='
237 . urlencode('DROP TABLE ' . PMA_Util::backquote($table)) . '">'
238 . $titles['Drop']
239 . '</a>'
240 . '</td>' . "\n";
242 $new_table_string .= '<td class="value">'
243 . $tbl_stats['Rows']
244 . '</td>' . "\n";
246 $new_table_string .= '<td class="nowrap">'
247 . $tbl_stats['Engine']
248 . '</td>' . "\n";
250 $new_table_string .= '<td>'
251 . '<dfn title="'
252 . PMA_getCollationDescr($tbl_stats['Collation']) . '">'
253 . $tbl_stats['Collation']
254 .'</dfn>'
255 . '</td>' . "\n";
257 if ($is_show_stats) {
258 $new_table_string .= '<td class="value tbl_size">'
259 . '<a href="tbl_structure.php'
260 . PMA_generate_common_url($tbl_url_params) . '#showusage" >'
261 . '<span>' . $formatted_size . '</span>'
262 . '<span class="unit">' . $unit . '</span>'
263 . '</a>'
264 . '</td>' . "\n" ;
266 $new_table_string .= '<td class="value tbl_overhead">'
267 . $overhead
268 . '</td>' . "\n" ;
270 $new_table_string .= '</tr>' . "\n";
272 $formatted_sql = PMA_Util::getMessage(
273 $message, $sql_query, 'success'
276 $response = PMA_Response::getInstance();
277 $response->addJSON('message', $message);
278 $response->addJSON('formatted_sql', $formatted_sql);
279 $response->addJSON('new_table_string', $new_table_string);
280 } else {
282 $display_query = $sql_query;
283 $sql_query = '';
285 // read table info on this newly created table, in case
286 // the next page is Structure
287 $reread_info = true;
288 include 'libraries/tbl_info.inc.php';
290 // do not switch to sql.php
291 // as there is no row to be displayed on a new table
292 if ($cfg['DefaultTabTable'] === 'sql.php') {
293 include 'tbl_structure.php';
294 } else {
295 include '' . $cfg['DefaultTabTable'];
298 } else {
299 if ($GLOBALS['is_ajax_request'] == true) {
300 $response = PMA_Response::getInstance();
301 $response->isSuccess(false);
302 $response->addJSON('message', $GLOBALS['dbi']->getError());
303 } else {
304 echo PMA_Util::mysqlDie('', '', '', $err_url, false);
305 // An error happened while inserting/updating a table definition.
306 // To prevent total loss of that data, we embed the form once again.
307 // The variable $regenerate will be used to restore data in
308 // libraries/tbl_columns_definition_form.inc.php
309 $num_fields = $_REQUEST['orig_num_fields'];
310 $regenerate = true;
313 exit;
314 } // end do create table
317 * Displays the form used to define the structure of the table
320 // This div is used to show the content(eg: create table form with more columns)
321 // fetched with AJAX subsequently.
322 if ($GLOBALS['is_ajax_request'] != true) {
323 echo('<div id="create_table_div">');
326 require 'libraries/tbl_columns_definition_form.inc.php';
328 if ($GLOBALS['is_ajax_request'] != true) {
329 echo('</div>');