I have made pages to make MySQL databases, and am having trouble getting the not null auto_increment primary key conditional statement to work, the script creates the database and tables, but ignores the auto-increment primary key sections and just assigns the column int. Any help would be great.
Code:
$db_name = "$POST[db_name]";
$connection = @mysql_connect("localhost", "user", "password")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "CREATE TABLE $POST[table_name] (";
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 ,";
}
}
$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>";
}
?>
form with input section:
<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=\"char\">varchar</option>
</select>
</td>
<td align=center><input type=\"text\" name=\"field_length[]\" size=\"5 \"></td>
<td ALIGN=CENTER><input type=\"checkbox\" name=\"primary[]\" value=\"Y \"></td>
<td ALIGN=CENTER><input type=\"checkbox\" name=\"auto_increment[]\" value=\"Y \"></td>
</tr>