Moksha Posted July 29, 2007 Share Posted July 29, 2007 Hi guys, im fairly new to php so go easy on me. I just bought teh book PHP 5 in easy steps, and im trying to run a script in it that creates a new database, and allows you then you create a table in that databse. The script from the book looks like this: <!-- example for PHP 5.0.0 final release --> <html><head><title>Creating a table</title></head> <body> <?php $self = $_SERVER['PHP_SELF']; $fields = $_POST['fields']; $db = $_POST['db']; $name = $_POST['name']; $table = $_POST['table']; $type = $_POST['type']; $size = $_POST['size']; if( !$fields and !$db ) { $form ="<form action=\"$self\" method=\"post\">"; $form.="How many fields are needed in the new table?<br>"; $form.="<input type=\"text\" name=\"fields\" size=\"5\">"; $form.="<input type=\"submit\" value=\"Submit\">"; echo($form); } else if( !$db ) { $form ="<form action=\"$self\" method=\"post\">"; $form.="Database: <input type=\"text\" name=\"db\"><br>"; $form.="Table Name: <input type=\"text\" name=\"table\" size=\"10\"><br> "; for ($i = 0 ; $i <$fields; $i++) { $form.="Column Name:<input type=\"text\" name=\"name[$i]\" size=\"10\"> "; $form.="Type: <select name=\"type[$i]\">"; $form.="<option value=\"char\">char</option>"; $form.="<option value=\"varchar\">varchar</option>"; $form.="<option value=\"int\">int</option>"; $form.="<option value=\"float\">float</option>"; $form.="<option value=\"timestamp\">timestamp</option>"; $form.="</select> "; $form.="Size:<input type=\"text\" name=\"size[$i]\" size=\"5\"><br>"; } $form.=" <input type=\"submit\" value=\"Submit\"></form>"; echo($form); } else { $conn = @mysql_connect("localhost", "root", "alpha") or die("Could not connect."); $rs = @mysql_select_db($db, $conn) or die("Could not select database."); $num_columns = count($name); $sql = "create table $table ("; for ($i = 0; $i < $num_columns; $i++) { $sql .= "$name[$i] $type[$i]"; if(($type[$i] =="char") or ($type[$i] =="varchar")) { if($size[$i] !="" ){ $sql.= "($size[$i])"; } } if(($i+1) != $num_columns){ $sql.=","; } } $sql .= ")"; echo("SQL COMMAND: $sql <hr>"); $result = mysql_query($sql,$conn) or die("Could not execute SQL query"); if ($result) { echo("RESULT: table \"$table\" has been created"); } } ?> </body></html> ------------------ i put this file called create_table.php in the directory of my server and try access it by typing localhost/create_table.php Immediately i get the following errors: Notice: Undefined index: fields in C:\HTTPServerDocs\create_table.php on line 8 Notice: Undefined index: db in C:\HTTPServerDocs\create_table.php on line 9 Notice: Undefined index: name in C:\HTTPServerDocs\create_table.php on line 10 Notice: Undefined index: table in C:\HTTPServerDocs\create_table.php on line 11 Notice: Undefined index: type in C:\HTTPServerDocs\create_table.php on line 12 Notice: Undefined index: size in C:\HTTPServerDocs\create_table.php on line 13 but it still allows me to input how many fields in the table. After i do this i follow the remaining instructiosn and create the database...then i receive the following error: Notice: Undefined index: fields in C:\HTTPServerDocs\create_table.php on line 8 Could not select database Is there soemthing wrong with teh php code, or something worng with my MySQL. Im pretty sure my MySQL is function fine as i can perform operations with other databases. Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 29, 2007 Share Posted July 29, 2007 Those are NOT errors, they are warnings. Warnings that you have various itrms undefined. One solution is to lower your script's error reporting level (the other is, of course, to define everything ) http://ca.php.net/manual/en/function.error-reporting.php Quote Link to comment Share on other sites More sharing options...
Moksha Posted July 29, 2007 Author Share Posted July 29, 2007 yeah i get what you mean by warnings.... but still at the end it says "could not select database". So maybe the database isnt getting created or soemthing. Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 29, 2007 Share Posted July 29, 2007 yeah i get what you mean by warnings.... but still at the end it says "could not select database". So maybe the database isnt getting created or soemthing. Yeah, that bit is an error. The code you have posted up (could you please use the [code[/code] tags! I have added them for you.) doesn't create a database, does the database that you are entering actually exist? Quote Link to comment 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.