Hello
I'm having trouble solving 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 '( )' at line 1
I have a php script which I've written in PHP6 which creates a table, in which the table name, and fields (and lengths if defined) are populated in a previous form. The object of the script is to allow a user to define different tables with different numbers and types of fields each time.
I have tried to play around with this and find where the script fails and I think it is to do with the 'for loop' and possibly something to do with the fact that my webserver only has PHP5 installed. If I take out the whole loop, I get the message to say my table has been successfully created, but it is obviously not showing any fields etc. However my PHP is not good enough to work out different syntax beteen different versions.....
Any help would be much appreciated.
<?php
$db_name = "iwalletc_testDB";
$connection = mysql_connect("localhost", "iwalletc", "qazxsw321") or die (mysql_error ());
$db = mysql_select_db($db_name, $connection) or die (mysql_error ());
//Start building the sql query
$sql = "CREATE TABLE $_POST[table_name] ( ";
//create a for loop to create the remainder of the query
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 .= ",";
}
}
$sql = substr ($sql, 0, -1);
$sql .= " )";
$result = mysql_query($sql, $connection) or die (mysql_error ());
if ($result) {
$msg = "<p>" .$_POST[table_name]." has been created!</p>";
}
?>