jaypizzy Posted April 20, 2007 Share Posted April 20, 2007 I get the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE animals)' at line 1 The code is a follows and the problem area is highlighted: <?php //Indicate the Database you want to use //$db_name ="testdb"; //Connect to the Database $connection = @mysql_connect("localhost","root","") or die(mysql_error()); //$db = @ mysql_select_db("testdb", $connection) or die(mysql_error()); //Start creating the Sql Statement $sql = "CREATE TABLE $_POST[table_name]("; //Continue the Sql statement for each new field for ($i = 0; $i < count($_POST[field_name]); $i++) { $sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i]; if($_POST[field_length][$i] !=""){ $sql .= "(".$_POST[field_length][$i]."),"; } else { $sql .=","; } //close of for loop } //Clean up the end of the string $sql.=substr($sql, 0, -1); $sql .=")"; //Execute the Query $result = mysql_query($sql, $connection) or die(mysql_error()); //Get a good message for display upon Success if ($result) { $msg = "<p>".$_POST[table_name]." has been created!</p>"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Create a Database Table: Step 3</title> </head> <body> <h1> Adding table to <? echo "$db_name"; ?>...</h1> <? echo "$msg"; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/47941-syntax-problem-please-help/ Share on other sites More sharing options...
per1os Posted April 20, 2007 Share Posted April 20, 2007 In the or die part print out the $sql and see what it is actuall builder. Next time use [ code ] tags they make it easier to read this mess. Link to comment https://forums.phpfreaks.com/topic/47941-syntax-problem-please-help/#findComment-234294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.