Jump to content

Inserting NULL values with a new entry


daviddth

Recommended Posts

OK another quick question on inserting values.

 

I have a database that contains various bits of info, but some items can have a NULL value, such as, in the example below DOD, POD, DOB & POB (Dates & places of birth). When I create a new entry, the null value is not set - I want it set IF the entry is empty.

 

<?php
$DOB = $_POST['DOB'];
$POB = $_POST['POB'];
// etc... Gets roughly 18 seperate values. If they are empty I tried to se them to null using
if (!$DOB) $DOB=NULL;
if (!$POB) $POB=NULL;

//but that did nothing when inserted - the items just contain nothing

$query = "INSERT INTO graves (FName, LName, DOB, POB, DOD, POD, SFName, SLName, S2FName, S2LName, S3FName, S3LName, DOBur, CemLocation, Image, Cemetery, ImageLoc, Female) VALUES ('$FName', '$LName', '$DOB', '$POB', '$DOD', '$POD', '$SFName', '$SLName', '$S2FName', '$S2LName', '$S3FName', '$S3LName', '$DOBur', '$CemLocation', '$Image', '$Cemetery', '$ImageLoc', '$Female')";
if(mysql_query($query)) echo '<h1>Updated</h1>';
  else echo '<h1>FAILED</h1>';
?>

 

I am sort of stumped, but obviously theres an issue with the way I try and tell the value it is NULL (I sort of think it's the ' around the $POD values). I have the same issue with the update query, but if this gets fixed the update one will be similar I guess :)

 

If I leave the items out of the query then the Null value is there (Because it's not putting '' in there), so is there a way I can "Build" a query based on if the value of each possibly NULL cell is not empty?

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/147507-inserting-null-values-with-a-new-entry/
Share on other sites

To insert something into MySQL as null, it goes like:

 

INSERT INTO table VALUES (NULL);

 

 

No quotes, and the string NULL.

 

 

In PHP, NULL is just NULL, not the string "NULL" which is what you need (except without quotes).

 

 

So uhhh.....  That probably made no sense to you at all since it's kind of hard to explain.

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.