将表单输入插入到数据库中,当为空时不插入为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. 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.

Not sure what to do and Google has not been helpful.

Thanks,

php
doulang9953
doulang9953 感谢某些原因无法找到。非常感激。
大约 6 年之前 回复
doufenzhu7012
doufenzhu7012 像这样:stackoverflow.com/a/4620409/2611927
大约 6 年之前 回复

1个回答

Just check the value like:

$website = "NULL";

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

.. insert to db

doumei1908
doumei1908 没有引号..澄清了我的答案..
大约 6 年之前 回复
donglei1973
donglei1973 用引号? 不要只是让变量等于字符串“null”
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐