ooo-Ghost-ooo ooo-Ghost-ooo - 6 days ago 6
PHP Question

PHP: insert NULL if input value is empty

i have been trying to insert NULL to my database if the value of input is empty
but i still it insert

empty and NULL string


Here's my code

this code returns blank

if (empty($_POST['caller'])){ $caller= NULL; }else{ $caller = mysqli_real_escape_string($con, $_POST['caller']);}

or this

$caller = mysqli_real_escape_string($con, $_POST['caller']);
$caller = !empty($caller_contact) ? "'$caller'" : NULL;


this code returns NULL string

$caller = mysqli_real_escape_string($con, $_POST['caller']);
$caller = !empty($caller_contact) ? "'$caller'" : 'NULL';

or this

if (empty($_POST['caller'])){ $caller= 'NULL'; }else{ $caller = mysqli_real_escape_string($con, $_POST['caller']);}


query

$sql_caller = "INSERT INTO `tblcall_info` VALUES ('','$save_inc_id','$call_time','$call_date','$caller','$caller_contact','$receiver','$device')";


i have also tried changing
'$caller'
to
$caller
but its an error.

enter image description here
can anyone help?. thanks

Answer

You can try this way:

remove single invated comma from query and set them to query it will surely work for you

if (empty($_POST['caller'])){ 
    $caller="NULL";
}else{ 
    $caller=mysqli_real_escape_string($con, $_POST['caller']);
    $caller="'$caller'";
}

$sql_caller = "INSERT INTO `tblcall_info` VALUES ('','$save_inc_id','$call_time','$call_date','$caller','$caller_contact','$receiver','$device')";
Comments