daviddth Posted March 2, 2009 Share Posted March 2, 2009 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 More sharing options...
corbin Posted March 2, 2009 Share Posted March 2, 2009 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. Link to comment https://forums.phpfreaks.com/topic/147507-inserting-null-values-with-a-new-entry/#findComment-774329 Share on other sites More sharing options...
daviddth Posted March 2, 2009 Author Share Posted March 2, 2009 I do get it, thanks. Now to work out how to convert it to code LOL. Then again I have been awake for 36 hrs which is not helping LOL Link to comment https://forums.phpfreaks.com/topic/147507-inserting-null-values-with-a-new-entry/#findComment-774376 Share on other sites More sharing options...
corbin Posted March 2, 2009 Share Posted March 2, 2009 lol ;p. Ternary syntax could cut down the length/clutter of the code. ((isset($soemthing)) ? "'" . mysql_real_escape_string($something) . "'" : 'NULL') Link to comment https://forums.phpfreaks.com/topic/147507-inserting-null-values-with-a-new-entry/#findComment-774397 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.