I am stumped with the code below. It is designed to create a table within a MySQL database called "Products". Creating the connection and selecting the database goes off without a hitch. However, I always get a "Couldn't execute query" error when I arrive at that stage. Any help would be greatly appreciated.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE>
</HEAD>
<BODY>
<H1>Adding Table <?php echo "$table_name"; ?></h1>
<?php
$sql = "CREATE TABLE $table_name (";
for ($i = 0; $i < count($field_name); $i++) {
$sql .= "$field_name[$i] $field_type[$i]";
if ($field_length[$i] != "") {
$sql .= " ($field_length[$i]),";
} else {
$sql .= ",";
}
}
$sql = substr($sql, 0, -1);
$sql .= ")";
// create connection
$connection = mysql_connect("localhost","sandman","password") or die("Couldn't connect to server.");
//select database
$db = mysql_select_db("Products",$connection) or die ("Couldn't select database.");
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection) or die ("Couldn't execute query.");
if (!$sql_result) {
echo "<p>Couldn't create table!";
} else {
echo "<P>$table_name has been created!";
}
?>
</BODY>