I’m trying to add a null value into a database if the form field is left blank but not getting much luck…
Can anyone see where this is going wrong??
<?php
$con = mysql_connect("ipaddress","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if (is_null($_POST["event_sub"]) || $_POST["event_sub"]=="") {
$event_sub = NULL;
} else {
$event_sub = mysql_real_escape_string($_POST["event_sub"]);
}
$sql="INSERT INTO myTable (event_sub)
VALUES
(". $event_sub .")";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
Perhaps try this:
Putting the NULL in single quotes will result in the word NULL being inserted into your query rather than an actual NULL, which is nothing.