/db/tbl_addfield.php

https://gitlab.com/a.loskutnikov/sitimobile · PHP · 211 lines · 166 code · 19 blank · 26 comment · 64 complexity · 5ba6359f7fa768c667b804cee526b183 MD5 · raw file

  1. <?php
  2. /* $Id: tbl_addfield.php,v 1.25.2.1 2002/04/28 11:46:58 loic1 Exp $ */
  3. /**
  4. * Get some core libraries
  5. */
  6. require('./libraries/grab_globals.lib.php');
  7. if (isset($submit)) {
  8. $js_to_run = 'functions.js';
  9. }
  10. require('./header.inc.php');
  11. /**
  12. * Defines the url to return to in case of error in a sql statement
  13. */
  14. $err_url = 'tbl_properties.php'
  15. . '?lang=' . $lang
  16. . '&amp;server=' . $server
  17. . '&amp;db=' . urlencode($db)
  18. . '&amp;table=' . urlencode($table);
  19. /**
  20. * The form used to define the field to add has been submitted
  21. */
  22. if (isset($submit)) {
  23. $query = '';
  24. // Transforms the radio button field_key into 3 arrays
  25. $field_cnt = count($field_name);
  26. for ($i = 0; $i < $field_cnt; ++$i) {
  27. if (isset(${'field_key_' . $i})) {
  28. if (${'field_key_' . $i} == 'primary_' . $i) {
  29. $field_primary[] = $i;
  30. }
  31. if (${'field_key_' . $i} == 'index_' . $i) {
  32. $field_index[] = $i;
  33. }
  34. if (${'field_key_' . $i} == 'unique_' . $i) {
  35. $field_unique[] = $i;
  36. }
  37. } // end if
  38. } // end for
  39. // Builds the field creation statement and alters the table
  40. for ($i = 0; $i < $field_cnt; ++$i) {
  41. if (get_magic_quotes_gpc()) {
  42. $field_name[$i] = stripslashes($field_name[$i]);
  43. }
  44. if (PMA_MYSQL_INT_VERSION < 32306) {
  45. PMA_checkReservedWords($field_name[$i], $err_url);
  46. }
  47. $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
  48. if ($field_length[$i] != ''
  49. && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
  50. if (get_magic_quotes_gpc()) {
  51. $query .= '(' . stripslashes($field_length[$i]) . ')';
  52. } else {
  53. $query .= '(' . $field_length[$i] . ')';
  54. }
  55. }
  56. if ($field_attribute[$i] != '') {
  57. $query .= ' ' . $field_attribute[$i];
  58. }
  59. if ($field_default[$i] != '') {
  60. if (strtoupper($field_default[$i]) == 'NULL') {
  61. $query .= ' DEFAULT NULL';
  62. } else if (get_magic_quotes_gpc()) {
  63. $query .= ' DEFAULT \'' . PMA_sqlAddslashes(stripslashes($field_default[$i])) . '\'';
  64. } else {
  65. $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
  66. }
  67. }
  68. if ($field_null[$i] != '') {
  69. $query .= ' ' . $field_null[$i];
  70. }
  71. if ($field_extra[$i] != '') {
  72. $query .= ' ' . $field_extra[$i];
  73. // An auto_increment field must be use as a primary key
  74. if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
  75. $primary_cnt = count($field_primary);
  76. for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
  77. // void
  78. } // end for
  79. if ($field_primary[$j] == $i) {
  80. $query .= ' PRIMARY KEY';
  81. unset($field_primary[$j]);
  82. } // end if
  83. } // end if (auto_increment)
  84. }
  85. if ($after_field != '--end--') {
  86. // Only the first field can be added somewhere else than at the end
  87. if ($i == 0) {
  88. if ($after_field == '--first--') {
  89. $query .= ' FIRST';
  90. } else {
  91. if (get_magic_quotes_gpc()) {
  92. $query .= ' AFTER ' . PMA_backquote(stripslashes(urldecode($after_field)));
  93. } else {
  94. $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
  95. }
  96. }
  97. } else {
  98. if (get_magic_quotes_gpc()) {
  99. $query .= ' AFTER ' . PMA_backquote(stripslashes($field_name[$i-1]));
  100. } else {
  101. $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
  102. }
  103. }
  104. }
  105. $query .= ', ADD ';
  106. } // end for
  107. $query = ereg_replace(', ADD $', '', $query);
  108. // To allow replication, we first select the db to use and then run queries
  109. // on this db.
  110. $sql_query = 'USE ' . PMA_backquote($db);
  111. $result = mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  112. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
  113. $result = mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  114. $sql_query_cpy = $sql_query . ';';
  115. // Builds the primary keys statements and updates the table
  116. $primary = '';
  117. if (isset($field_primary)) {
  118. $primary_cnt = count($field_primary);
  119. for ($i = 0; $i < $primary_cnt; $i++) {
  120. $j = $field_primary[$i];
  121. $primary .= PMA_backquote($field_name[$j]) . ', ';
  122. } // end for
  123. $primary = ereg_replace(', $', '', $primary);
  124. if (!empty($primary)) {
  125. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')';
  126. $result = mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  127. $sql_query_cpy .= "\n" . $sql_query . ';';
  128. }
  129. } // end if
  130. // Builds the indexes statements and updates the table
  131. $index = '';
  132. if (isset($field_index)) {
  133. $index_cnt = count($field_index);
  134. for ($i = 0; $i < $index_cnt; $i++) {
  135. $j = $field_index[$i];
  136. $index .= PMA_backquote($field_name[$j]) . ', ';
  137. } // end for
  138. $index = ereg_replace(', $', '', $index);
  139. if (!empty($index)) {
  140. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
  141. $result = mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  142. $sql_query_cpy .= "\n" . $sql_query . ';';
  143. }
  144. } // end if
  145. // Builds the uniques statements and updates the table
  146. $unique = '';
  147. if (isset($field_unique)) {
  148. $unique_cnt = count($field_unique);
  149. for ($i = 0; $i < $unique_cnt; $i++) {
  150. $j = $field_unique[$i];
  151. $unique .= PMA_backquote($field_name[$j]) . ', ';
  152. } // end for
  153. $unique = ereg_replace(', $', '', $unique);
  154. if (!empty($unique)) {
  155. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
  156. $result = mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  157. $sql_query_cpy .= "\n" . $sql_query . ';';
  158. }
  159. } // end if
  160. // Builds the fulltext statements and updates the table
  161. $fulltext = '';
  162. if (PMA_MYSQL_INT_VERSION >= 32323 && isset($field_fulltext)) {
  163. $fulltext_cnt = count($field_fulltext);
  164. for ($i = 0; $i < $fulltext_cnt; $i++) {
  165. $j = $field_fulltext[$i];
  166. $fulltext .= PMA_backquote($field_name[$j]) . ', ';
  167. } // end for
  168. $fulltext = ereg_replace(', $', '', $fulltext);
  169. if (!empty($fulltext)) {
  170. $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
  171. $result = mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  172. $sql_query_cpy .= "\n" . $sql_query . ';';
  173. }
  174. } // end if
  175. // Go back to table properties
  176. $sql_query = $sql_query_cpy;
  177. unset($sql_query_cpy);
  178. $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
  179. include('./tbl_properties.php');
  180. exit();
  181. } // end do alter table
  182. /**
  183. * Displays the form used to define the new field
  184. */
  185. else{
  186. $action = 'tbl_addfield.php';
  187. include('./tbl_properties.inc.php');
  188. // Diplays the footer
  189. echo "\n";
  190. include('./footer.inc.php');
  191. }
  192. ?>