PHP/MySQL:如何防止插入所有列= NULL 的行?
我正在尝试使用 此触发器 以 PHP 形式。我已经尝试了以下操作,但是当我提交表单时,仍然会在表中插入空行。
$trigger="DELIMITER $$
CREATE TRIGGER check_not_all_null BEFORE INSERT ON agent FOR EACH ROW
BEGIN
IF COALESCE(first_name, last_name, affiliation) IS NOT NULL THEN
CALL fail('All fields cannot be null');
END IF;
END $$
DELIMITER ; ";
$execute = mysql_query($trigger);
$query4 = "INSERT INTO agent
(first_name, last_name, affiliation)
VALUES
('$first_name', '$last_name', '$affiliation')";
$result4 = mysql_query($query4, $connection) or die('Error querying database.');
I'm trying to use this trigger in a PHP form. I've tried the following, but I still get null rows inserted into my table when I submit the form.
$trigger="DELIMITER $
CREATE TRIGGER check_not_all_null BEFORE INSERT ON agent FOR EACH ROW
BEGIN
IF COALESCE(first_name, last_name, affiliation) IS NOT NULL THEN
CALL fail('All fields cannot be null');
END IF;
END $
DELIMITER ; ";
$execute = mysql_query($trigger);
$query4 = "INSERT INTO agent
(first_name, last_name, affiliation)
VALUES
('$first_name', '$last_name', '$affiliation')";
$result4 = mysql_query($query4, $connection) or die('Error querying database.');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我认为您的意思是
因为
NOT NULL
意味着其中一个值(至少)不为空。其次,@tofutim 是正确的;空字符串与 NULL 值不同。
First off, I think you mean
because
NOT NULL
would mean that one of the values (at least) was not null.Second, @tofutim is correct; empty strings are not the same as
NULL
values.如果我理解你的逻辑不应该是这一行:
Be
否则,只有当存在不为空的值时,你才会失败。
If I understand your logic shouldn't this line:
Be
Otherwise, you'll fail only when there is a value that isn't null.
触发器需要指定
new.
虚拟表,否则它将永远无法工作。您可能需要考虑将触发器更改为。
这将强制将空字符串放入
null
中。请参阅: http://dev.mysql.com /doc/refman/5.0/en/control-flow-functions.html#function_nullif
并且: http://dev.mysql.com/doc/refman /5.5/en/create-trigger.html
The trigger needs to have the
new.
virtual table specified otherwise it will never work.You might want to consider changing your trigger into.
This will force empty strings into
null
's.See: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_nullif
And: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
假设您的触发器根据已提供的优秀答案进行了修改,可以保证集合 {first_name, last_name, affiliation} 中的至少一个属性不为空。假设您还有一个 ID 号代理键,该表可能允许这样的数据。 (我不知道你所说的从属关系是什么意思,所以我只使用 char(1) 来实现。)
假设你的名字是“Christian Guilbert”。这些行中只有一行(代理人 ID 号之一)是您的。是哪一个?
想想这是否真的是你想做的。我对于数据库约束的第一条经验法则是:数据库管理系统允许的所有内容最终都会进入您的数据库。 (我曾经看到财富 500 强 IT 部门发布了一款应用程序,允许用户输入运输集装箱的负尺寸。这样您就可以将很多箱子放入火车车厢中。在纸上。)
恕我直言,这三列中的每一列都应该被声明为 NOT NULL。我也会为所有这么说的人投票。
(名称由随机名称生成器提供。)
Let's say that your trigger, as modified according to the excellent answers already provided, works to guarantee that at least one attribute of the set {first_name, last_name, affiliation} is not null. Assuming you also have a ID number surrogate key, that table might allow data like this. (I don't know what you mean by affiliation, so I'll just use a char(1) for that.)
Let's say that your name is "Christian Guilbert". Only one of those rows (one of those surrogate ID number) is yours. Which one is it?
Think about whether this is really what you want to do. My first rule of thumb for database constraints: everything the dbms allows will eventually find its way into your database. (I once saw a Fortune 500 IT department release an app that allowed users to enter negative dimensions for shipping containers. You can fit a lot of boxes into a railroad car that way. On paper.)
IMHO, every one of those three columns should be declared NOT NULL. And I'm gonna upvote everyone else who says that, too.
(Names courtesy of The Random Name Generator.)