Jump to content

Conditional Statement Help


MikeL7

Recommended Posts

I have made pages to create databases and the db gets created, but if i specify a not null auto_increment ,primary key the script ignores it and just makes the column a int column. Any help to make the script work would ge great. I am using php 5.2.3 here is the 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>";

}

?>

 

Heres the form section that send the primary and auto_increment choices:

<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>

Link to comment
https://forums.phpfreaks.com/topic/56119-conditional-statement-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.