Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / tbl_addfield.php
blob4b2c3fe216fdee60c319e6f7cd94b6c5e5503cba
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays add field form and handles it
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get some core libraries
12 require_once 'libraries/common.inc.php';
14 $response = PMA_Response::getInstance();
15 $header = $response->getHeader();
16 $scripts = $header->getScripts();
17 $scripts->addFile('tbl_structure.js');
19 // Check parameters
20 PMA_Util::checkParameters(array('db', 'table'));
23 /**
24 * Defines the url to return to in case of error in a sql statement
26 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
28 /**
29 * The form used to define the field to add has been submitted
31 $abort = false;
33 // check number of fields to be created
34 if (isset($_REQUEST['submit_num_fields'])) {
35 if (isset($_REQUEST['orig_after_field'])) {
36 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
38 if (isset($_REQUEST['orig_field_where'])) {
39 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
41 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
42 $regenerate = true;
43 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
44 $num_fields = (int) $_REQUEST['num_fields'];
45 } else {
46 $num_fields = 1;
49 if (isset($_REQUEST['do_save_data'])) {
50 //avoid an incorrect calling of PMA_updateColumns() via
51 //tbl_structure.php below
52 unset($_REQUEST['do_save_data']);
54 include_once 'libraries/create_addfield.lib.php';
55 // get column addition statements
56 $sql_statement = PMA_getColumnCreationStatements(false);
58 // To allow replication, we first select the db to use and then run queries
59 // on this db.
60 $GLOBALS['dbi']->selectDb($db)
61 or PMA_Util::mysqlDie(
62 $GLOBALS['dbi']->getError(),
63 'USE ' . PMA_Util::backquote($db), '',
64 $err_url
66 $sql_query = 'ALTER TABLE ' .
67 PMA_Util::backquote($table) . ' ' . $sql_statement . ';';
68 $result = $GLOBALS['dbi']->tryQuery($sql_query);
70 if ($result === true) {
71 // If comments were sent, enable relation stuff
72 include_once 'libraries/transformations.lib.php';
74 // Update comment table for mime types [MIME]
75 if (isset($_REQUEST['field_mimetype'])
76 && is_array($_REQUEST['field_mimetype'])
77 && $cfg['BrowseMIME']
78 ) {
79 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
80 if (isset($_REQUEST['field_name'][$fieldindex])
81 && strlen($_REQUEST['field_name'][$fieldindex])
82 ) {
83 PMA_setMIME(
84 $db, $table,
85 $_REQUEST['field_name'][$fieldindex],
86 $mimetype,
87 $_REQUEST['field_transformation'][$fieldindex],
88 $_REQUEST['field_transformation_options'][$fieldindex]
94 // Go back to the structure sub-page
95 $message = PMA_Message::success(
96 __('Table %1$s has been altered successfully')
98 $message->addParam($table);
100 if ($GLOBALS['is_ajax_request'] == true) {
101 $response->addJSON('message', $message);
102 $response->addJSON(
103 'sql_query',
104 PMA_Util::getMessage(null, $sql_query)
106 exit;
109 $active_page = 'tbl_structure.php';
110 $abort = true;
111 include 'tbl_structure.php';
112 } else {
113 $error_message_html = PMA_Util::mysqlDie('', '', '', $err_url, false);
114 $response->addHTML($error_message_html);
115 if ($GLOBALS['is_ajax_request'] == true) {
116 exit;
118 // An error happened while inserting/updating a table definition.
119 // to prevent total loss of that data, we embed the form once again.
120 // The variable $regenerate will be used to restore data in libraries/
121 // tbl_columns_definition_form.inc.php
122 $num_fields = $_REQUEST['orig_num_fields'];
123 if (isset($_REQUEST['orig_after_field'])) {
124 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
126 if (isset($_REQUEST['orig_field_where'])) {
127 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
129 $regenerate = true;
131 } // end do alter table
134 * Displays the form used to define the new field
136 if ($abort == false) {
138 * Gets tables informations
140 include_once 'libraries/tbl_common.inc.php';
141 include_once 'libraries/tbl_info.inc.php';
143 $active_page = 'tbl_structure.php';
145 * Display the form
147 $action = 'tbl_addfield.php';
148 include_once 'libraries/tbl_columns_definition_form.inc.php';