1

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

2 Answers 2

2

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')";
Sign up to request clarification or add additional context in comments.

8 Comments

its just like my code. it will only generate string null not the actual NULL
please check my updated comment it may helps you :-|
as in my statement above in my question i already change '$caller' to $caller but it generated an error
i accepted it coz it got me an idea and tried it and it work
i just put single quotes in null like this 'NULL' and remove quotes to $caller
|
1

Try this method in the $sql_caller string:

INSERT INTO table_name (column1,column2,...)  VALUES(value1,value2,...);

And skip the column & it's value, which you want to be set as NULL.

1 Comment

if i skip column and value that i want to be NULL then the problem is if the value is not empty it will not be added to the database. it will always be null even if it is not empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.