tbl_create.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * @package PhpMyAdmin
  5. */
  6. /**
  7. * Get some core libraries
  8. */
  9. require_once 'libraries/common.inc.php';
  10. $action = 'tbl_create.php';
  11. $titles = PMA_Util::buildActionTitles();
  12. // Check parameters
  13. PMA_Util::checkParameters(array('db'));
  14. /* Check if database name is empty */
  15. if (strlen($db) == 0) {
  16. PMA_Util::mysqlDie(
  17. __('The database name is empty!'), '', '', 'index.php'
  18. );
  19. }
  20. /**
  21. * Defines the url to return to in case of error in a sql statement
  22. */
  23. if (PMA_DBI_get_columns($db, $table)) {
  24. // table exists already
  25. PMA_Util::mysqlDie(
  26. sprintf(__('Table %s already exists!'), htmlspecialchars($table)),
  27. '',
  28. '',
  29. 'db_structure.php?' . PMA_generate_common_url($db)
  30. );
  31. }
  32. $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
  33. // check number of fields to be created
  34. if (isset($_REQUEST['submit_num_fields'])) {
  35. $regenerate = true; // for libraries/tbl_columns_definition_form.inc.php
  36. $num_fields = min(
  37. intval($_REQUEST['orig_num_fields']) + intval($_REQUEST['added_fields']),
  38. 4096
  39. );
  40. } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
  41. $num_fields = min(4096, intval($_REQUEST['num_fields']));
  42. } else {
  43. $num_fields = 4;
  44. }
  45. /**
  46. * Selects the database to work with
  47. */
  48. if (!PMA_DBI_select_db($db)) {
  49. PMA_Util::mysqlDie(
  50. sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
  51. '',
  52. '',
  53. 'index.php'
  54. );
  55. }
  56. /**
  57. * The form used to define the structure of the table has been submitted
  58. */
  59. if (isset($_REQUEST['do_save_data'])) {
  60. $sql_query = '';
  61. // Transforms the radio button field_key into 3 arrays
  62. $field_cnt = count($_REQUEST['field_name']);
  63. for ($i = 0; $i < $field_cnt; ++$i) {
  64. if (isset($_REQUEST['field_key'][$i])) {
  65. if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
  66. $field_primary[] = $i;
  67. }
  68. if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
  69. $field_index[] = $i;
  70. }
  71. if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
  72. $field_unique[] = $i;
  73. }
  74. } // end if
  75. } // 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])
  80. && $_REQUEST['field_name'][$i] != '0'
  81. ) {
  82. continue;
  83. }
  84. $query = PMA_Table::generateFieldSpec(
  85. $_REQUEST['field_name'][$i],
  86. $_REQUEST['field_type'][$i],
  87. $i,
  88. $_REQUEST['field_length'][$i],
  89. $_REQUEST['field_attribute'][$i],
  90. isset($_REQUEST['field_collation'][$i])
  91. ? $_REQUEST['field_collation'][$i]
  92. : '',
  93. isset($_REQUEST['field_null'][$i])
  94. ? $_REQUEST['field_null'][$i]
  95. : 'NOT NULL',
  96. $_REQUEST['field_default_type'][$i],
  97. $_REQUEST['field_default_value'][$i],
  98. isset($_REQUEST['field_extra'][$i])
  99. ? $_REQUEST['field_extra'][$i]
  100. : false,
  101. isset($_REQUEST['field_comments'][$i])
  102. ? $_REQUEST['field_comments'][$i]
  103. : '',
  104. $field_primary,
  105. ''
  106. );
  107. $query .= ', ';
  108. $sql_query .= $query;
  109. } // end for
  110. unset($field_cnt, $query);
  111. $sql_query = preg_replace('@, $@', '', $sql_query);
  112. // Builds the primary keys statements
  113. $primary = '';
  114. $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
  115. for ($i = 0; $i < $primary_cnt; $i++) {
  116. $j = $field_primary[$i];
  117. if (isset($_REQUEST['field_name'][$j])
  118. && strlen($_REQUEST['field_name'][$j])
  119. ) {
  120. $primary .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
  121. }
  122. } // end for
  123. unset($primary_cnt);
  124. $primary = preg_replace('@, $@', '', $primary);
  125. if (strlen($primary)) {
  126. $sql_query .= ', PRIMARY KEY (' . $primary . ')';
  127. }
  128. unset($primary);
  129. // Builds the indexes statements
  130. $index = '';
  131. $index_cnt = (isset($field_index) ? count($field_index) : 0);
  132. for ($i = 0;$i < $index_cnt; $i++) {
  133. $j = $field_index[$i];
  134. if (isset($_REQUEST['field_name'][$j])
  135. && strlen($_REQUEST['field_name'][$j])
  136. ) {
  137. $index .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
  138. }
  139. } // end for
  140. unset($index_cnt);
  141. $index = preg_replace('@, $@', '', $index);
  142. if (strlen($index)) {
  143. $sql_query .= ', INDEX (' . $index . ')';
  144. }
  145. unset($index);
  146. // Builds the uniques statements
  147. $unique = '';
  148. $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
  149. for ($i = 0; $i < $unique_cnt; $i++) {
  150. $j = $field_unique[$i];
  151. if (isset($_REQUEST['field_name'][$j])
  152. && strlen($_REQUEST['field_name'][$j])
  153. ) {
  154. $unique .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
  155. }
  156. } // end for
  157. unset($unique_cnt);
  158. $unique = preg_replace('@, $@', '', $unique);
  159. if (strlen($unique)) {
  160. $sql_query .= ', UNIQUE (' . $unique . ')';
  161. }
  162. unset($unique);
  163. // Builds the FULLTEXT statements
  164. $fulltext = '';
  165. $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
  166. for ($i = 0; $i < $fulltext_cnt; $i++) {
  167. $j = $field_fulltext[$i];
  168. if (isset($_REQUEST['field_name'][$j])
  169. && strlen($_REQUEST['field_name'][$j])
  170. ) {
  171. $fulltext .= PMA_Util::backquote($_REQUEST['field_name'][$j]) . ', ';
  172. }
  173. } // end for
  174. $fulltext = preg_replace('@, $@', '', $fulltext);
  175. if (strlen($fulltext)) {
  176. $sql_query .= ', FULLTEXT (' . $fulltext . ')';
  177. }
  178. unset($fulltext);
  179. // Builds the 'create table' statement
  180. $sql_query = 'CREATE TABLE ' . PMA_Util::backquote($db) . '.'
  181. . PMA_Util::backquote($table) . ' (' . $sql_query . ')';
  182. // Adds table type, character set, comments and partition definition
  183. if (!empty($_REQUEST['tbl_storage_engine'])
  184. && ($_REQUEST['tbl_storage_engine'] != 'Default')
  185. ) {
  186. $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_storage_engine'];
  187. }
  188. if (!empty($_REQUEST['tbl_collation'])) {
  189. $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
  190. }
  191. if (!empty($_REQUEST['comment'])) {
  192. $sql_query .= ' COMMENT = \''
  193. . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
  194. }
  195. if (!empty($_REQUEST['partition_definition'])) {
  196. $sql_query .= ' ' . PMA_Util::sqlAddSlashes(
  197. $_REQUEST['partition_definition']
  198. );
  199. }
  200. $sql_query .= ';';
  201. // Executes the query
  202. $result = PMA_DBI_try_query($sql_query);
  203. if ($result) {
  204. // If comments were sent, enable relation stuff
  205. include_once 'libraries/transformations.lib.php';
  206. // Update comment table for mime types [MIME]
  207. if (isset($_REQUEST['field_mimetype'])
  208. && is_array($_REQUEST['field_mimetype'])
  209. && $cfg['BrowseMIME']
  210. ) {
  211. foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
  212. if (isset($_REQUEST['field_name'][$fieldindex])
  213. && strlen($_REQUEST['field_name'][$fieldindex])
  214. ) {
  215. PMA_setMIME(
  216. $db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
  217. $_REQUEST['field_transformation'][$fieldindex],
  218. $_REQUEST['field_transformation_options'][$fieldindex]
  219. );
  220. }
  221. }
  222. }
  223. $message = PMA_Message::success(__('Table %1$s has been created.'));
  224. $message->addParam(
  225. PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table)
  226. );
  227. if ($GLOBALS['is_ajax_request'] == true) {
  228. /**
  229. * construct the html for the newly created table's row to be appended
  230. * to the list of tables.
  231. *
  232. * Logic taken from db_structure.php
  233. */
  234. $tbl_url_params = array();
  235. $tbl_url_params['db'] = $db;
  236. $tbl_url_params['table'] = $table;
  237. $is_show_stats = $cfg['ShowStats'];
  238. $tbl_stats_result = PMA_DBI_query(
  239. 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($db)
  240. . ' LIKE \'' . PMA_Util::sqlAddSlashes($table, true) . '\';'
  241. );
  242. $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
  243. PMA_DBI_free_result($tbl_stats_result);
  244. unset($tbl_stats_result);
  245. if ($is_show_stats) {
  246. $sum_size = (double) 0;
  247. $overhead_size = (double) 0;
  248. $overhead_check = '';
  249. $tblsize = doubleval($tbl_stats['Data_length'])
  250. + doubleval($tbl_stats['Index_length']);
  251. $sum_size += $tblsize;
  252. list($formatted_size, $unit) = PMA_Util::formatByteDown(
  253. $tblsize,
  254. 3,
  255. ($tblsize > 0) ? 1 : 0
  256. );
  257. if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
  258. list($formatted_overhead, $overhead_unit) = PMA_Util::formatByteDown(
  259. $tbl_stats['Data_free'],
  260. 3,
  261. ($tbl_stats['Data_free'] > 0) ? 1 : 0
  262. );
  263. $overhead_size += $tbl_stats['Data_free'];
  264. }
  265. if (isset($formatted_overhead)) {
  266. $overhead = '<span>' . $formatted_overhead . '</span>'
  267. . '<span class="unit">' . $overhead_unit . '</span>';
  268. unset($formatted_overhead);
  269. } else {
  270. $overhead = '-';
  271. }
  272. }
  273. $new_table_string = '<tr>' . "\n";
  274. $new_table_string .= '<td class="center">'
  275. . '<input type="checkbox" id="checkbox_tbl_"'
  276. . ' name="selected_tbl[]" value="'.htmlspecialchars($table).'" />'
  277. . '</td>' . "\n";
  278. $new_table_string .= '<th>';
  279. $new_table_string .= '<a href="sql.php'
  280. . PMA_generate_common_url($tbl_url_params) . '">'
  281. . htmlspecialchars($table) . '</a>';
  282. if (PMA_Tracker::isActive()) {
  283. $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
  284. if (PMA_Tracker::isTracked($db, $truename)) {
  285. $new_table_string .= '<a href="tbl_tracking.php'
  286. . PMA_generate_common_url($tbl_url_params) . '">';
  287. $new_table_string .= PMA_Util::getImage(
  288. 'eye.png', __('Tracking is active.')
  289. );
  290. } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
  291. $new_table_string .= '<a href="tbl_tracking.php'
  292. . PMA_generate_common_url($tbl_url_params) . '">';
  293. $new_table_string .= PMA_Util::getImage(
  294. 'eye_grey.png', __('Tracking is not active.')
  295. );
  296. }
  297. unset($truename);
  298. }
  299. $new_table_string .= '</th>' . "\n";
  300. $new_table_string .= '<td>' . $titles['NoBrowse'] . '</td>' . "\n";
  301. $new_table_string .= '<td>'
  302. . '<a href="tbl_structure.php'
  303. . PMA_generate_common_url($tbl_url_params) . '">'
  304. . $titles['Structure']
  305. . '</a>'
  306. . '</td>' . "\n";
  307. $new_table_string .= '<td>' . $titles['NoSearch'] . '</td>' . "\n";
  308. $new_table_string .= '<td>'
  309. . '<a href="tbl_change.php'
  310. . PMA_generate_common_url($tbl_url_params) . '">'
  311. . $titles['Insert']
  312. . '</a>'
  313. . '</td>' . "\n";
  314. $new_table_string .= '<td>' . $titles['NoEmpty'] . '</td>' . "\n";
  315. $new_table_string .= '<td>'
  316. . '<a class="drop_table_anchor" href="sql.php'
  317. . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query='
  318. . urlencode('DROP TABLE ' . PMA_Util::backquote($table)) . '">'
  319. . $titles['Drop']
  320. . '</a>'
  321. . '</td>' . "\n";
  322. $new_table_string .= '<td class="value">'
  323. . $tbl_stats['Rows']
  324. . '</td>' . "\n";
  325. $new_table_string .= '<td class="nowrap">'
  326. . $tbl_stats['Engine']
  327. . '</td>' . "\n";
  328. $new_table_string .= '<td>'
  329. . '<dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'
  330. . $tbl_stats['Collation']
  331. .'</dfn>'
  332. . '</td>' . "\n";
  333. if ($is_show_stats) {
  334. $new_table_string .= '<td class="value tbl_size">'
  335. . '<a href="tbl_structure.php'
  336. . PMA_generate_common_url($tbl_url_params) . '#showusage" >'
  337. . '<span>' . $formatted_size . '</span>'
  338. . '<span class="unit">' . $unit . '</span>'
  339. . '</a>'
  340. . '</td>' . "\n" ;
  341. $new_table_string .= '<td class="value tbl_overhead">'
  342. . $overhead
  343. . '</td>' . "\n" ;
  344. }
  345. $new_table_string .= '</tr>' . "\n";
  346. $formatted_sql = PMA_Util::getMessage(
  347. $message, $sql_query, 'success'
  348. );
  349. $response = PMA_Response::getInstance();
  350. $response->addJSON('message', $message);
  351. $response->addJSON('formatted_sql', $formatted_sql);
  352. $response->addJSON('new_table_string', $new_table_string);
  353. } else {
  354. $display_query = $sql_query;
  355. $sql_query = '';
  356. // read table info on this newly created table, in case
  357. // the next page is Structure
  358. $reread_info = true;
  359. include 'libraries/tbl_info.inc.php';
  360. // do not switch to sql.php
  361. // as there is no row to be displayed on a new table
  362. if ($cfg['DefaultTabTable'] === 'sql.php') {
  363. include 'tbl_structure.php';
  364. } else {
  365. include '' . $cfg['DefaultTabTable'];
  366. }
  367. }
  368. } else {
  369. if ($GLOBALS['is_ajax_request'] == true) {
  370. $response = PMA_Response::getInstance();
  371. $response->isSuccess(false);
  372. $response->addJSON('message', PMA_DBI_getError());
  373. } else {
  374. echo PMA_Util::mysqlDie('', '', '', $err_url, false);
  375. // An error happened while inserting/updating a table definition.
  376. // To prevent total loss of that data, we embed the form once again.
  377. // The variable $regenerate will be used to restore data in
  378. // libraries/tbl_columns_definition_form.inc.php
  379. $num_fields = $_REQUEST['orig_num_fields'];
  380. $regenerate = true;
  381. }
  382. }
  383. exit;
  384. } // end do create table
  385. /**
  386. * Displays the form used to define the structure of the table
  387. */
  388. // This div is used to show the content(eg: create table form with more columns)
  389. // fetched with AJAX subsequently.
  390. if ($GLOBALS['is_ajax_request'] != true) {
  391. echo('<div id="create_table_div">');
  392. }
  393. require 'libraries/tbl_columns_definition_form.inc.php';
  394. if ($GLOBALS['is_ajax_request'] != true) {
  395. echo('</div>');
  396. }
  397. ?>