Here is the code where I am recieving my error.
do_createtable.php
PHP Code:
<?
//indicate the database you want to use
$db_name = "testDB";
//connect to database
$connection = @mysql_connect("localhost","breen","area") or die(mysql_error());
$db = @mysql_select_db($db_name, $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[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] != "") {
$sql .= " (".$_POST[field_length][$i].") $additional ,";
} else {
$sql .= " $additional ,";
}
}
//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>";
}
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE>
</HEAD>
<BODY>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</BODY>
</HTML>
The error I get is:
You have an error in your SQL syntax near ')' at line 1
Obviously, my code is dying. What it should be doing is adding a table to "testDB". I use a form page where I enter in the field name, the field type, the field length, and whether it is a primary key and/or auto increment. Then I click the submit button where it should create my table no problem. I get this error instead.
If anyone could point me in the right direction, that would be great.
Thanks.