3.1.3.2 release
[phpmyadmin/crack.git] / tbl_alter.php
blob873b76c91502fe45f15bf9da4fa7b61662a05e7c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Alter one or more table columns/fields
6 * linked from table_structure, uses libraries/tbl_properties.inc.php to display
7 * form and handles this form data
9 * @version $Id$
12 /**
13 * Gets some core libraries
15 require_once './libraries/common.inc.php';
16 require_once './libraries/Table.class.php';
18 $GLOBALS['js_include'][] = 'functions.js';
19 require_once './libraries/header.inc.php';
21 // Check parameters
22 PMA_checkParameters(array('db', 'table'));
24 /**
25 * Gets tables informations
27 require_once './libraries/tbl_common.php';
28 require_once './libraries/tbl_info.inc.php';
30 $active_page = 'tbl_structure.php';
32 /**
33 * Defines the url to return to in case of error in a sql statement
35 $err_url = 'tbl_structure.php?' . PMA_generate_common_url($db, $table);
38 /**
39 * Modifications have been submitted -> updates the table
41 $abort = false;
42 if (isset($_REQUEST['do_save_data'])) {
43 $field_cnt = count($_REQUEST['field_orig']);
44 $key_fields = array();
45 $changes = array();
47 for ($i = 0; $i < $field_cnt; $i++) {
48 $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
49 $_REQUEST['field_orig'][$i],
50 $_REQUEST['field_name'][$i],
51 $_REQUEST['field_type'][$i],
52 $_REQUEST['field_length'][$i],
53 $_REQUEST['field_attribute'][$i],
54 isset($_REQUEST['field_collation'][$i])
55 ? $_REQUEST['field_collation'][$i]
56 : '',
57 isset($_REQUEST['field_null'][$i])
58 ? $_REQUEST['field_null'][$i]
59 : 'NOT NULL',
60 $_REQUEST['field_default_type'][$i],
61 $_REQUEST['field_default_value'][$i],
62 isset($_REQUEST['field_extra'][$i])
63 ? $_REQUEST['field_extra'][$i]
64 : false,
65 isset($_REQUEST['field_comments'][$i])
66 ? $_REQUEST['field_comments'][$i]
67 : '',
68 $key_fields,
69 $i,
70 $_REQUEST['field_default_orig'][$i]
72 } // end for
74 // Builds the primary keys statements and updates the table
75 $key_query = '';
76 /**
77 * this is a little bit more complex
79 * @todo if someone selects A_I when altering a column we need to check:
80 * - no other column with A_I
81 * - the column has an index, if not create one
83 if (count($key_fields)) {
84 $fields = array();
85 foreach ($key_fields as $each_field) {
86 if (isset($_REQUEST['field_name'][$each_field]) && strlen($_REQUEST['field_name'][$each_field])) {
87 $fields[] = PMA_backquote($_REQUEST['field_name'][$each_field]);
89 } // end for
90 $key_query = ', ADD KEY (' . implode(', ', $fields) . ') ';
94 // To allow replication, we first select the db to use and then run queries
95 // on this db.
96 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
97 // Optimization fix - 2 May 2001 - Robbat2
98 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $changes) . $key_query;
99 $result = PMA_DBI_try_query($sql_query);
101 if ($result !== false) {
102 $message = PMA_Message::success('strTableAlteredSuccessfully');
103 $message->addParam($table);
104 $btnDrop = 'Fake';
106 // garvin: If comments were sent, enable relation stuff
107 require_once './libraries/relation.lib.php';
108 require_once './libraries/transformations.lib.php';
110 // updaet field names in relation
111 if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
112 foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
113 if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
114 PMA_REL_renameField($db, $table, $fieldcontent,
115 $_REQUEST['field_name'][$fieldindex]);
120 // update mime types
121 if (isset($_REQUEST['field_mimetype'])
122 && is_array($_REQUEST['field_mimetype'])
123 && $cfg['BrowseMIME']) {
124 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
125 if (isset($_REQUEST['field_name'][$fieldindex])
126 && strlen($_REQUEST['field_name'][$fieldindex])) {
127 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex],
128 $mimetype,
129 $_REQUEST['field_transformation'][$fieldindex],
130 $_REQUEST['field_transformation_options'][$fieldindex]);
135 $active_page = 'tbl_structure.php';
136 require './tbl_structure.php';
137 } else {
138 PMA_mysqlDie('', '', '', $err_url, false);
139 // garvin: An error happened while inserting/updating a table definition.
140 // to prevent total loss of that data, we embed the form once again.
141 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
142 if (isset($_REQUEST['orig_field'])) {
143 $_REQUEST['field'] = $_REQUEST['orig_field'];
146 $regenerate = true;
151 * No modifications yet required -> displays the table fields
153 * $selected comes from multi_submits.inc.php
155 if ($abort == false) {
156 if (! isset($selected)) {
157 PMA_checkParameters(array('field'));
158 $selected[] = $_REQUEST['field'];
159 $selected_cnt = 1;
160 } else { // from a multiple submit
161 $selected_cnt = count($selected);
165 * @todo optimize in case of multiple fields to modify
167 for ($i = 0; $i < $selected_cnt; $i++) {
168 $_REQUEST['field'] = PMA_sqlAddslashes($selected[$i], true);
169 $result = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $_REQUEST['field'] . '\';');
170 $fields_meta[] = PMA_DBI_fetch_assoc($result);
171 PMA_DBI_free_result($result);
173 $num_fields = count($fields_meta);
174 $action = 'tbl_alter.php';
176 // Get more complete field information
177 // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
178 // but later, if the analyser returns more information, it
179 // could be executed for any MySQL version and replace
180 // the info given by SHOW FULL FIELDS FROM.
182 * @todo put this code into a require()
183 * or maybe make it part of PMA_DBI_get_fields();
186 if (PMA_MYSQL_INT_VERSION < 50025) {
187 // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
188 // SHOW FULL FIELDS says NULL and SHOW CREATE TABLE says NOT NULL (tested
189 // in MySQL 4.0.25).
191 $show_create_table = PMA_DBI_fetch_value(
192 'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
193 0, 1);
194 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
197 require './libraries/tbl_properties.inc.php';
202 * Displays the footer
204 require_once './libraries/footer.inc.php';