Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / tbl_create.php
blob9397b87453f2fbe51938c22213ea596646e02966
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(sprintf(__('Table %s already exists!'), htmlspecialchars($table)), '',
31 '', 'db_structure.php?' . PMA_generate_common_url($db));
34 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
36 // check number of fields to be created
37 if (isset($_REQUEST['submit_num_fields'])) {
38 $regenerate = true; // for libraries/tbl_properties.inc.php
39 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
40 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
41 $num_fields = (int) $_REQUEST['num_fields'];
42 } else {
43 $num_fields = 4;
46 /**
47 * Selects the database to work with
49 if (!PMA_DBI_select_db($db)) {
50 PMA_mysqlDie(sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
51 '', '', 'main.php');
54 /**
55 * The form used to define the structure of the table has been submitted
57 if (isset($_REQUEST['do_save_data'])) {
58 $sql_query = '';
60 // Transforms the radio button field_key into 3 arrays
61 $field_cnt = count($_REQUEST['field_name']);
62 for ($i = 0; $i < $field_cnt; ++$i) {
63 if (isset($_REQUEST['field_key'][$i])) {
64 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
65 $field_primary[] = $i;
67 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
68 $field_index[] = $i;
70 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
71 $field_unique[] = $i;
73 } // end if
74 } // end for
76 // Builds the fields creation statements
77 for ($i = 0; $i < $field_cnt; $i++) {
78 // '0' is also empty for php :-(
79 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
80 continue;
83 $query = PMA_Table::generateFieldSpec(
84 $_REQUEST['field_name'][$i],
85 $_REQUEST['field_type'][$i],
86 $_REQUEST['field_length'][$i],
87 $_REQUEST['field_attribute'][$i],
88 isset($_REQUEST['field_collation'][$i])
89 ? $_REQUEST['field_collation'][$i]
90 : '',
91 isset($_REQUEST['field_null'][$i])
92 ? $_REQUEST['field_null'][$i]
93 : 'NOT NULL',
94 $_REQUEST['field_default_type'][$i],
95 $_REQUEST['field_default_value'][$i],
96 isset($_REQUEST['field_extra'][$i])
97 ? $_REQUEST['field_extra'][$i]
98 : false,
99 isset($_REQUEST['field_comments'][$i])
100 ? $_REQUEST['field_comments'][$i]
101 : '',
102 $field_primary,
103 $i);
105 $query .= ', ';
106 $sql_query .= $query;
107 } // end for
108 unset($field_cnt, $query);
109 $sql_query = preg_replace('@, $@', '', $sql_query);
111 // Builds the primary keys statements
112 $primary = '';
113 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
114 for ($i = 0; $i < $primary_cnt; $i++) {
115 $j = $field_primary[$i];
116 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
117 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
119 } // end for
120 unset($primary_cnt);
121 $primary = preg_replace('@, $@', '', $primary);
122 if (strlen($primary)) {
123 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
125 unset($primary);
127 // Builds the indexes statements
128 $index = '';
129 $index_cnt = (isset($field_index) ? count($field_index) : 0);
130 for ($i = 0;$i < $index_cnt; $i++) {
131 $j = $field_index[$i];
132 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
133 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
135 } // end for
136 unset($index_cnt);
137 $index = preg_replace('@, $@', '', $index);
138 if (strlen($index)) {
139 $sql_query .= ', INDEX (' . $index . ')';
141 unset($index);
143 // Builds the uniques statements
144 $unique = '';
145 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
146 for ($i = 0; $i < $unique_cnt; $i++) {
147 $j = $field_unique[$i];
148 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
149 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
151 } // end for
152 unset($unique_cnt);
153 $unique = preg_replace('@, $@', '', $unique);
154 if (strlen($unique)) {
155 $sql_query .= ', UNIQUE (' . $unique . ')';
157 unset($unique);
159 // Builds the FULLTEXT statements
160 $fulltext = '';
161 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
162 for ($i = 0; $i < $fulltext_cnt; $i++) {
163 $j = $field_fulltext[$i];
164 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
165 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
167 } // end for
169 $fulltext = preg_replace('@, $@', '', $fulltext);
170 if (strlen($fulltext)) {
171 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
173 unset($fulltext);
175 // Builds the 'create table' statement
176 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
177 . ' (' . $sql_query . ')';
179 // Adds table type, character set, comments and partition definition
180 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
181 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
183 if (!empty($_REQUEST['tbl_collation'])) {
184 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
186 if (!empty($_REQUEST['comment'])) {
187 $sql_query .= ' COMMENT = \'' . PMA_sqlAddSlashes($_REQUEST['comment']) . '\'';
189 if (!empty($_REQUEST['partition_definition'])) {
190 $sql_query .= ' ' . PMA_sqlAddSlashes($_REQUEST['partition_definition']);
192 $sql_query .= ';';
194 // Executes the query
195 $result = PMA_DBI_try_query($sql_query);
197 if ($result) {
199 // If comments were sent, enable relation stuff
200 include_once './libraries/transformations.lib.php';
202 // Update comment table for mime types [MIME]
203 if (isset($_REQUEST['field_mimetype'])
204 && is_array($_REQUEST['field_mimetype'])
205 && $cfg['BrowseMIME']) {
206 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
207 if (isset($_REQUEST['field_name'][$fieldindex])
208 && strlen($_REQUEST['field_name'][$fieldindex])) {
209 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
210 $_REQUEST['field_transformation'][$fieldindex],
211 $_REQUEST['field_transformation_options'][$fieldindex]);
216 $message = PMA_Message::success(__('Table %1$s has been created.'));
217 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
219 if ($GLOBALS['is_ajax_request'] == true) {
222 * construct the html for the newly created table's row to be appended
223 * to the list of tables.
225 * Logic taken from db_structure.php
228 $tbl_url_params = array();
229 $tbl_url_params['db'] = $db;
230 $tbl_url_params['table'] = $table;
231 $is_show_stats = $cfg['ShowStats'];
233 $tbl_stats_result = PMA_DBI_query('SHOW TABLE STATUS FROM '
234 . PMA_backquote($db) . ' LIKE \'' . PMA_sqlAddSlashes($table, true) . '\';');
235 $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
236 PMA_DBI_free_result($tbl_stats_result);
237 unset($tbl_stats_result);
239 if ($is_show_stats) {
240 $sum_size = (double) 0;
241 $overhead_size = (double) 0;
242 $overhead_check = '';
244 $tblsize = doubleval($tbl_stats['Data_length']) + doubleval($tbl_stats['Index_length']);
245 $sum_size += $tblsize;
246 list($formatted_size, $unit) = PMA_formatByteDown($tblsize, 3, ($tblsize > 0) ? 1 : 0);
247 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
248 list($formatted_overhead, $overhead_unit) = PMA_formatByteDown($tbl_stats['Data_free'], 3, ($tbl_stats['Data_free'] > 0) ? 1 : 0);
249 $overhead_size += $tbl_stats['Data_free'];
252 if (isset($formatted_overhead)) {
253 $overhead = $formatted_overhead . ' ' . $overhead_unit;
254 unset($formatted_overhead);
255 } else {
256 $overhead = '-';
260 $new_table_string = '<tr>' . "\n";
261 $new_table_string .= '<td align="center"> <input type="checkbox" id="checkbox_tbl_" name="selected_tbl[]" value="'.htmlspecialchars($table).'" /> </td>' . "\n";
263 $new_table_string .= '<th>';
264 $new_table_string .= '<a href="sql.php' . PMA_generate_common_url($tbl_url_params) . '">'. $table . '</a>';
266 if (PMA_Tracker::isActive()) {
267 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
268 if (PMA_Tracker::isTracked($db, $truename)) {
269 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '">';
270 $new_table_string .= PMA_getImage('eye.png', __('Tracking is active.'));
271 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
272 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '">';
273 $new_table_string .= PMA_getImage('eye_grey.png', __('Tracking is not active.'));
275 unset($truename);
277 $new_table_string .= '</th>' . "\n";
279 $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
281 $new_table_string .= '<td><a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Structure'] . '</a></td>' . "\n";
283 $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
285 $new_table_string .= '<td><a href="tbl_change.php' . PMA_generate_common_url($tbl_url_params) . '">' . $titles['Insert'] . '</a></td>' . "\n";
287 $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
289 $new_table_string .= '<td><a class="drop_table_anchor" href="sql.php' . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query=';
290 $new_table_string .= urlencode('DROP TABLE ' . PMA_backquote($table));
291 $new_table_string .= '">';
292 $new_table_string .= $titles['Drop'];
293 $new_table_string .= '</a></td>' . "\n";
295 $new_table_string .= '<td class="value">' . $tbl_stats['Rows'] . '</td>' . "\n";
297 $new_table_string .= '<td nowrap="nowrap">' . $tbl_stats['Engine'] . '</td>' . "\n";
299 $new_table_string .= '<td> <dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'. $tbl_stats['Collation'] .'</dfn></td>' . "\n";
301 if ($is_show_stats) {
302 $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" ;
303 $new_table_string .= '<td class="value">' . $overhead . '</td>' . "\n" ;
306 $new_table_string .= '</tr>' . "\n";
308 $extra_data['new_table_string'] = $new_table_string;
310 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
313 $display_query = $sql_query;
314 $sql_query = '';
316 // read table info on this newly created table, in case
317 // the next page is Structure
318 $reread_info = true;
319 include './libraries/tbl_info.inc.php';
321 // do not switch to sql.php - as there is no row to be displayed on a new table
322 if ($cfg['DefaultTabTable'] === 'sql.php') {
323 include './tbl_structure.php';
324 } else {
325 include './' . $cfg['DefaultTabTable'];
327 exit;
328 } else {
329 if ($GLOBALS['is_ajax_request'] == true) {
330 PMA_ajaxResponse(PMA_DBI_getError(), false);
331 } else {
332 PMA_mysqlDie('', '', '', $err_url, false);
333 // An error happened while inserting/updating a table definition.
334 // to prevent total loss of that data, we embed the form once again.
335 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
336 $num_fields = $_REQUEST['orig_num_fields'];
337 $regenerate = true;
340 } // end do create table
343 * Displays the form used to define the structure of the table
346 // This div is used to show the content(eg: create table form with more columns) fetched with AJAX subsequently.
347 if ($GLOBALS['is_ajax_request'] != true) {
348 echo('<div id="create_table_div">');
351 require './libraries/tbl_properties.inc.php';
352 // Displays the footer
353 require './libraries/footer.inc.php';
355 if ($GLOBALS['is_ajax_request'] != true) {
356 echo('</div>');