gpt4 book ai didi

php - cakephp - Mysql 中的空白记录问题

转载 作者:行者123 更新时间:2023-11-29 02:34:56 25 4
gpt4 key购买 nike

在我的应用程序中,尽管在客户端和服务器端都进行了严格的验证,但不知何故,用户注册页面的记录会变成空白。所有数据都是空白的,接受我表中的默认值(例如性别 - 男性)。大约每 100 个注册中就有 1 个会发生这种情况。

Mysql - 对于每个必填字段,我都有类似 not null 的结构。仍然是插入空白。

Cakephp 方面 - 我正在检查数据数组在保存之前是否为空。

如果有人能帮助我,我将不胜感激。

提前致谢。

最佳答案

它是空白的,因为在每个数据库(Oracle 除外)中,空字符串 '' 都不是 null。因此数据库将接受它。

如果你想防止这种情况发生,你需要像这样编写一个触发器:

DELIMITER $$

CREATE TRIGGER bi_table1_each BEFORE INSERT ON table1 FOR EACH ROW
BEGIN
//this will cause an error and prevent the insertion of an empty gender field.
//If gender has a default value, the database will insert the default value instead.
IF new.gender = '' THEN SET new.gender = null;
END $$

DELIMITER ;

或者,您可以将性别设置为枚举字段

ALTER table1 
CHANGE COLUMN gender gender ENUM ('M','F');

链接:
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://dev.mysql.com/doc/refman/5.5/en/triggers.html

关于php - cakephp - Mysql 中的空白记录问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6692270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com