|
Posted by Christopher Pomasl on 10/06/78 11:25
On Tue, 30 Aug 2005 11:52:35 +0200, johan wrote:
> On Mon, 29 Aug 2005 11:24:55 -0400, Dave Thomas wrote:
>
>> If I have a table set up like this:
>>
>> Name | VARCHAR
>> Email | VARCHAR
>> Age | TINYINT | NULL (Default: NULL)
>>
>> And I want the user to enter his or her name, email, and age - but AGE
>> is optional.
>>
>> My insert would look something like:
>>
>> INSERT INTO data (Name, Email, Age) VALUES ('$name', '$email', $age)
>>
>> This is all good, except if the user doesn't enter an age. Then $age is
>> an empty variable. I thought that since the table is set up where Age
>> can be NULL, that this should be fine. But MySQL is giving me an error.
>> If $age is a number, it is no problem.
>>
>> Anyway to make it accept $age as an empty variable (and just make it
>> NULL)? I know I could say:
>>
>> if (empty($age)) { $age = 0; }
>>
>> and write it that way, but this is just an example and there are many
>> more variables that could be empty.
>>
>> Thanks.
>
>
> BTW, a field containg an AGE has no real value. What will happen net year.
> Youd'better take care of the birth day, a fixed data.
>
> Johan
This is a response to the OP....
When I have items that are nullable, I build the list and the values
sections as I parse the page. So if itemx is there, I build the DB_ItemX
field into the the ongoing column list:
(DB_ItemA, DB_ItemB, DB_ItemX)
This is relatively easy using:
$insertList.= ',DB_ItemX';
and then it's value in the value list:
$valueList.= ",'".$formItemX."'";
So when you get to the point of building the runnable SQL:
$insertSQL = "INSERT to your.table (";
$insertSQL.= $insertList ;
$insertSQL.= ") Values (";
$insertSQL.= $valueList ;
$insertSQL.= ")" ;
Also note that if you are supplying values for all of the columns in the
table, SQL does not require the column list. The values then need to be
in the exact same order as defined in the table though!!
Chris
Navigation:
[Reply to this message]
|