doudong4532 2011-11-05 14:55
浏览 25
已采纳

无法保存类型INT的空值(php,symfony)

I have a question here. I have a website running on symfony 1.0. I used the symfony admin generator tool to generate an interface to edit information in the database. On the edit page, I have some textfields that require integer values to be saved in the database. The problem that I'm experiencing is that, if I don't put anything in the textfields that require integer numbers and hit SAVE, the value of ZERO is saved in the database (instead of null). Keep in mind that in the structure of the database, the type of the columns where the integers are saved are type INT(11) DEFAULT NULL. So, normally, NULL should be saved if I don't enter anything.

Any idea what might go wrong please?

  • 写回答

1条回答 默认 最新

  • dtt5830659 2011-11-05 15:01
    关注

    Yes, form fields are always Strings. So when you submit them without inputting anything, you are actually submitting '' (an empty string) which gets translated into 0. You should include a simple condition to make sure the input is not empty, and take appropriate action if it does.

    if (empty($_POST['input'])) { $variable_to_pass = null; }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大