MySQL Create Table Problem
| From: | Jeff Oien | Date: | Fri, 02 Mar 2001 19:09:24 +0000 |
| Subject: | MySQL Create Table Problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-42383@lists.php.net to get a copy of this message | ||
This is driving me nuts. I really tried to figure it out
myself. I have a form for creating a table. The form
fields have field_name field_type field_length, and
checkboxes for not_null, auto_increment and primary.
When I check the primary key checkbox it always makes
the first field the primary key no matter what. And it
makes two date fields I enter NULL and the rest NOT
NULL no matter what I put in the checkboxes. The code
is below along with a printout of the SQL query. I only
wanted suffix and death date to be null. This is combination
from PHP Fast and Easy and MySQL by Dubois.
$sql = "CREATE TABLE $table_name (";
for ($i = 0; $i < count($field_name); $i++) {
$sql .= "$field_name[$i] $field_type[$i]";
if ($not_null[$i] == "Y") {
$additional = " NOT NULL";
} else {
$additional = " NULL";
}
if ($auto_increment[$i] == "Y") {
$additional .= " AUTO_INCREMENT";
} else {
$additional .= "";
}
if ($primary[$i] == "Y") {
$additional .= " primary key";
} else {
$additional .= "";
}
if ($field_length[$i] != "") {
$sql .= "($field_length[$i]) $additional,";
} else {
$sql .= " $additional,";
}
}
$sql = substr($sql, 0, -1);
$sql .= ")";
------------
CREATE TABLE president (last_name varchar(15) NOT NULL primary key,first_name
varchar(15) NOT NULL,suffix varchar(5) NOT NULL,city varchar(20) NOT NULL,state
varchar(2) NOT NULL,birth date NULL,death date NULL)