tbl_addfield.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. * Get some core libraries
  9. */
  10. require_once 'libraries/common.inc.php';
  11. $response = PMA_Response::getInstance();
  12. $header = $response->getHeader();
  13. $scripts = $header->getScripts();
  14. $scripts->addFile('tbl_structure.js');
  15. // Check parameters
  16. PMA_Util::checkParameters(array('db', 'table'));
  17. /**
  18. * Defines the url to return to in case of error in a sql statement
  19. */
  20. $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
  21. /**
  22. * The form used to define the field to add has been submitted
  23. */
  24. $abort = false;
  25. // check number of fields to be created
  26. if (isset($_REQUEST['submit_num_fields'])) {
  27. if (isset($_REQUEST['orig_after_field'])) {
  28. $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
  29. }
  30. if (isset($_REQUEST['orig_field_where'])) {
  31. $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
  32. }
  33. $num_fields = min(
  34. intval($_REQUEST['orig_num_fields']) + intval($_REQUEST['added_fields']),
  35. 4096
  36. );
  37. $regenerate = true;
  38. } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
  39. $num_fields = min(4096, intval($_REQUEST['num_fields']));
  40. } else {
  41. $num_fields = 1;
  42. }
  43. if (isset($_REQUEST['do_save_data'])) {
  44. //avoid an incorrect calling of PMA_updateColumns() via
  45. //tbl_structure.php below
  46. unset($_REQUEST['do_save_data']);
  47. $query = '';
  48. $definitions = array();
  49. // Transforms the radio button field_key into 3 arrays
  50. $field_cnt = count($_REQUEST['field_name']);
  51. $field_primary = array();
  52. $field_index = array();
  53. $field_unique = array();
  54. $field_fulltext = array();
  55. for ($i = 0; $i < $field_cnt; ++$i) {
  56. if (isset($_REQUEST['field_key'][$i])
  57. && strlen($_REQUEST['field_name'][$i])
  58. ) {
  59. if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
  60. $field_primary[] = $i;
  61. }
  62. if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
  63. $field_index[] = $i;
  64. }
  65. if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
  66. $field_unique[] = $i;
  67. }
  68. if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
  69. $field_fulltext[] = $i;
  70. }
  71. } // end if
  72. } // end for
  73. // Builds the field creation statement and alters the table
  74. for ($i = 0; $i < $field_cnt; ++$i) {
  75. // '0' is also empty for php :-(
  76. if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
  77. continue;
  78. }
  79. $definition = ' ADD ' . PMA_Table::generateFieldSpec(
  80. $_REQUEST['field_name'][$i],
  81. $_REQUEST['field_type'][$i],
  82. $i,
  83. $_REQUEST['field_length'][$i],
  84. $_REQUEST['field_attribute'][$i],
  85. isset($_REQUEST['field_collation'][$i])
  86. ? $_REQUEST['field_collation'][$i]
  87. : '',
  88. isset($_REQUEST['field_null'][$i])
  89. ? $_REQUEST['field_null'][$i]
  90. : 'NOT NULL',
  91. $_REQUEST['field_default_type'][$i],
  92. $_REQUEST['field_default_value'][$i],
  93. isset($_REQUEST['field_extra'][$i])
  94. ? $_REQUEST['field_extra'][$i]
  95. : false,
  96. isset($_REQUEST['field_comments'][$i])
  97. ? $_REQUEST['field_comments'][$i]
  98. : '',
  99. $field_primary
  100. );
  101. if ($_REQUEST['field_where'] != 'last') {
  102. // Only the first field can be added somewhere other than at the end
  103. if ($i == 0) {
  104. if ($_REQUEST['field_where'] == 'first') {
  105. $definition .= ' FIRST';
  106. } else {
  107. $definition .= ' AFTER ' . PMA_Util::backquote($_REQUEST['after_field']);
  108. }
  109. } else {
  110. $definition .= ' AFTER ' . PMA_Util::backquote($_REQUEST['field_name'][$i-1]);
  111. }
  112. }
  113. $definitions[] = $definition;
  114. } // end for
  115. // Builds the primary keys statements and updates the table
  116. if (count($field_primary)) {
  117. $fields = array();
  118. foreach ($field_primary as $field_nr) {
  119. $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
  120. }
  121. $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
  122. unset($fields);
  123. }
  124. // Builds the indexes statements and updates the table
  125. if (count($field_index)) {
  126. $fields = array();
  127. foreach ($field_index as $field_nr) {
  128. $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
  129. }
  130. $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
  131. unset($fields);
  132. }
  133. // Builds the uniques statements and updates the table
  134. if (count($field_unique)) {
  135. $fields = array();
  136. foreach ($field_unique as $field_nr) {
  137. $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
  138. }
  139. $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
  140. unset($fields);
  141. }
  142. // Builds the fulltext statements and updates the table
  143. if (count($field_fulltext)) {
  144. $fields = array();
  145. foreach ($field_fulltext as $field_nr) {
  146. $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
  147. }
  148. $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
  149. unset($fields);
  150. }
  151. // To allow replication, we first select the db to use and then run queries
  152. // on this db.
  153. PMA_DBI_select_db($db) or PMA_Util::mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_Util::backquote($db), '', $err_url);
  154. $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ' . implode(', ', $definitions) . ';';
  155. $result = PMA_DBI_try_query($sql_query);
  156. if ($result === true) {
  157. // If comments were sent, enable relation stuff
  158. include_once 'libraries/transformations.lib.php';
  159. // Update comment table for mime types [MIME]
  160. if (isset($_REQUEST['field_mimetype'])
  161. && is_array($_REQUEST['field_mimetype'])
  162. && $cfg['BrowseMIME']
  163. ) {
  164. foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
  165. if (isset($_REQUEST['field_name'][$fieldindex])
  166. && strlen($_REQUEST['field_name'][$fieldindex])
  167. ) {
  168. PMA_setMIME(
  169. $db, $table,
  170. $_REQUEST['field_name'][$fieldindex],
  171. $mimetype,
  172. $_REQUEST['field_transformation'][$fieldindex],
  173. $_REQUEST['field_transformation_options'][$fieldindex]
  174. );
  175. }
  176. }
  177. }
  178. // Go back to the structure sub-page
  179. $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
  180. $message->addParam($table);
  181. if ($GLOBALS['is_ajax_request'] == true) {
  182. $response->addJSON('message', $message);
  183. $response->addJSON(
  184. 'sql_query',
  185. PMA_Util::getMessage(null, $sql_query)
  186. );
  187. exit;
  188. }
  189. $active_page = 'tbl_structure.php';
  190. $abort = true;
  191. include 'tbl_structure.php';
  192. } else {
  193. $error_message_html = PMA_Util::mysqlDie('', '', '', $err_url, false);
  194. $response->addHTML($error_message_html);
  195. if ($GLOBALS['is_ajax_request'] == true) {
  196. exit;
  197. }
  198. // An error happened while inserting/updating a table definition.
  199. // to prevent total loss of that data, we embed the form once again.
  200. // The variable $regenerate will be used to restore data in libraries/tbl_columns_definition_form.inc.php
  201. $num_fields = $_REQUEST['orig_num_fields'];
  202. if (isset($_REQUEST['orig_after_field'])) {
  203. $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
  204. }
  205. if (isset($_REQUEST['orig_field_where'])) {
  206. $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
  207. }
  208. $regenerate = true;
  209. }
  210. } // end do alter table
  211. /**
  212. * Displays the form used to define the new field
  213. */
  214. if ($abort == false) {
  215. /**
  216. * Gets tables informations
  217. */
  218. include_once 'libraries/tbl_common.inc.php';
  219. include_once 'libraries/tbl_info.inc.php';
  220. $active_page = 'tbl_structure.php';
  221. /**
  222. * Display the form
  223. */
  224. $action = 'tbl_addfield.php';
  225. include_once 'libraries/tbl_columns_definition_form.inc.php';
  226. }
  227. ?>