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>";
}
	
?>

Can you give us the listing of your form from which the field names are being populated? I would like to look at the fields in the form to correlate them with the fields in the php code to see what's wrong.

Hi Thanks for replying..sure here is the initial form in which the user just creates the table name and specifies the number of fields required:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create a Database Table: Step 1</title>
</head>

<body>
<h1>Step 1: Name and Number</h1>
<form method="POST" action="do_showfielddef.php">
<p><strong> Table Name </strong><br>
<input type ="text" name ="table_name" size=30</p>
<p><strong>Number of Fields</strong><br>
<input type ="text" name="num_fields" size=5></p>
<p><input type ="submit" name="submit" value="Go to Step 2"></p>
</form>
</body>
</html>

This is the first script (do_showfielddef.php) called by the form:

<?php 
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
	header ("Location: show_createtable.htm");
	exit;
}
//begin creating form for display
$form_block ="
<FORM METHOD=\"POST\" ACTION=\"do_createtable.php\">
<input type=\"hidden\" name=\"table_name\" value=\"$_POST[table_name]\">
<TABLE CELLSPACING=5 CELLPADDING=5>
<tr>
<th>Field Name</th><th>Field Length</th></tr>";

//count from 0 until you reach the number of fields
FOR ($i = 0; $i <$_POST[num_fields]; $i++) {
	//add to the form, one row for each field
	$form_block .= "
	<TR>
	<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"field_name []\" SIZE=\"30\"></TD>
	<TD ALIGN=CENTER>
	<SELECT NAME=\"field_type[]\">
	<OPTION VALUE=\"char\">CHAR</OPTION>
	<OPTION VALUE=\"date\">DATE</OPTION>
	<OPTION VALUE=\"float\">FLOAT</OPTION>
	<OPTION VALUE=\"int\">INT</OPTION>
	<OPTION VALUE=\"text\">TEXT</OPTION>
	<OPTION VALUE=\"varchar\">VARCHAR</OPTION>
	</SELECT>
	</TD>
	
	<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"field_length[]\" SIZE=\"5\"></TD>
	</TR>";
}
//close the for loop
$form_block .= "
<tr>
<td align=center colspan=3><input type=\"submit\" value=\"Create Table\"></td>
</tr>
</table>
</form>";

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.