Basically I am having a lot of trouble figuring out how I can assign NULL to a variable and then send NULL to mysql.
The case is I want to edit some users, and the filed date of birth is a mandatory field, so if they update it then OK, but if they do not it stays NULL.
Now in my edit function I have a varibale that say:
$born_on = $_POST['born_on'], which is basically an empty textbox.
when I update mysql it saves that is 00-00-0000 00:00:00
Now even when I put an if condition making $born_on = NULL it still saves the 00-00-0000 00:00:00, how can I change that back to a NULL whenever I make the textbox empty without having to write an extra query just for that field in an if condition.
Don't confuse the PHP null with the SQL NULL. When you use the PHP null in a string (such as your SQL string) it will be cast to a zero or empty string (I don't remember which off the top of my head). But to set a column's value to null in SQL, you need to use the NULL keyword in the query. So very simplistically (with no error-checking or SQL injection escaping), you might do something like:
// note use of string listeral "NULL" instead of PHP null...
// ...plus adding single quotes around the actual value if supplied
$born_on = (empty($_POST['born_on'])) ? "NULL" : "'".$_POST['born_on']."'";
$query = "INSERT INTO `table_x` (`born_on`) VALUES ($born_on)";
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Date of birth is not a mandatory field, but I don't want to make different SQL scripts considering cases where date of birth is assigned / date of birth not assigned. Because this is one of many non-mandatory fields basically.
The issue I am facing is that basically I need to somehow make a php variable equal exactly to the MYSQL null basically.