mdmartiny Posted March 15, 2012 Share Posted March 15, 2012 Hello my fellow PHP developers. I am writing a script that will allow for someone to create a table in their database but just filling in a few text boxes. I am coming across the problem Notice: Undefined offset: 1. I know what it means and all but I just do not know how to fix it. So if someone would be so kind to help me I would appreciate it. <?php $table_name = $_POST['table_name']; $create_sql = "create table $table_name ("; for ($i = 0; $i < count($_POST['field_name']); $i++) { $create_sql .= $_POST['field_name'][$i] . " " . $_POST['field_type'][$i]; if($_POST['auto_increment'][$i] == "y" ) { $additional = "NOT NULL auto_increment"; } else { $additional = ""; } if($_POST['primary'][$i] == "y"){ $additional .= ", primary key(".$_POST['field_name'][$i].")"; } else { $additional = ""; } if ($_POST['field_length'][$i] != "") { $create_sql .= "(" . $_POST['field_length'][$i] . ") $additional,"; } else { $create_sql .= " $additional ,"; } } $create_sql = substr($create_sql, 0, -1); $create_sql .= ")"; $create_res = mysql_query($create_sql); if ($create_res) { $sql_column = "SHOW COLUMNS FROM $table_name"; $res_column = mysql_query($sql_column); echo "<h3>the following database has been created <strong>". $table_name. "</strong></h3>"; if (mysql_num_rows($res_column)) { echo "<table class='db-table' cellpadding='5' cellspacing='5'>"; echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>"; while ($row = mysql_fetch_row($res_column)) { echo "<tr>"; foreach ($row as $key => $value) { echo "<td>". $value ."</td>"; } echo "</tr>"; } echo "</table><br />"; } echo "<a href='create_table.php'>Create</a> another table"; } else { echo "<h3>There was an error" . mysql_error()."</h3>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/ Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 Place this code at the top of the page. echo "<pre>"; print_r($_POST); echo "</pre>"; This will display the $_POST values being submitted to the page via form. My guess is that one or more of the post values you are trying to access as arrays are in fact strings. Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1327604 Share on other sites More sharing options...
mdmartiny Posted March 16, 2012 Author Share Posted March 16, 2012 I find the issue to be with the $_POST(auto_increment) and $_POST(primary). It adds the null and primary factor. Then when it moves onto yhe next grips in the array that is when I get the error. Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328004 Share on other sites More sharing options...
AyKay47 Posted March 16, 2012 Share Posted March 16, 2012 Post the results from the code I gave you. Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328023 Share on other sites More sharing options...
mdmartiny Posted March 17, 2012 Author Share Posted March 17, 2012 This is the output from the code Array ( [table_name] => mike [field_name] => Array ( [0] => hggghghbwthw [1] => test [2] => test_field ) [field_type] => Array ( [0] => int [1] => char [2] => varchar ) [field_length] => Array ( [0] => 5 [1] => 50 [2] => 50 ) [primary] => Array ( [0] => y ) [auto_increment] => Array ( [0] => y ) ) Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328306 Share on other sites More sharing options...
mdmartiny Posted March 17, 2012 Author Share Posted March 17, 2012 This is the output from the code Array ( [table_name] => mike [field_name] => Array ( [0] => hggghghbwthw [1] => test [2] => test_field ) [field_type] => Array ( [0] => int [1] => char [2] => varchar ) [field_length] => Array ( [0] => 5 [1] => 50 [2] => 50 ) [primary] => Array ( [0] => y ) [auto_increment] => Array ( [0] => y ) ) Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328308 Share on other sites More sharing options...
AyKay47 Posted March 17, 2012 Share Posted March 17, 2012 should be pretty obvious at this point. The internal arrays [primary] and [auto_increment] only contain 1 key/value pair, so trying to put these in a for loop and access any further key/value pairs will trip an error (the one you received). e.g. $_POST['auto_increment'][1] does not exist, only $_POST['auto_increment'][0] does. Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328333 Share on other sites More sharing options...
mdmartiny Posted March 17, 2012 Author Share Posted March 17, 2012 I seem that after I actually took the time to look at it. Was in a rush this morning. Quote Link to comment https://forums.phpfreaks.com/topic/258975-why-array-why/#findComment-1328337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.