简体   繁体   English

将表单输入插入数据库,当为空时不将其插入为NULL

[英]Inserting form input into database and when empty doesnt insert as NULL

I have a form and everything works well. 我有表格,一切正常。 However when I escape the input using mysqli real_escape_string, if the input was left blank by the user, it enters blank in the database and not as NULL. 但是,当我使用mysqli real_escape_string对输入进行转义时,如果用户将输入留空,则它将在数据库中输入空白而不是NULL。 here is the code I have for the variable. 这是变量的代码。

    $website = '"'.$mysqli->real_escape_string($_POST['website']).'"';

The variables that are not escaped , ie a variable that is numeric and has only 7 chars appears NULL if nothing is input by the user. 如果用户未输入任何内容,则不会转义的变量(即,只有7个字符的数字变量)显示为NULL。

Not sure what to do and Google has not been helpful. 不确定该怎么做,Google对此没有帮助。

Thanks, 谢谢,

Just check the value like: 只需检查以下值:

$website = "NULL";

if (isset($_POST['website']))
  $website = "'" . $mysqli->real_escape_string($_POST['website']) . "'";

.. insert to db ..插入数据库

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

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