简体   繁体   English

PHP:如果输入值为空,则插入NULL

[英]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 我一直在尝试向我的数据库插入NULL ,如果输入的值为空但我仍然插入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 此代码返回NULL字符串

  $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. 我也尝试将'$caller'更改为$caller但这是一个错误。

在此输入图像描述 can anyone help?. 任何人都可以帮忙吗? thanks 谢谢

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')";

Try this method in the $sql_caller string: 在$ sql_caller字符串中尝试此方法:

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

And skip the column & it's value, which you want to be set as NULL. 并跳过列和它的值,您希望将其设置为NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM