PHP/MySQL:如何防止插入所有列= NULL 的行?

发布于 2024-11-10 08:34:43 字数 731 浏览 0 评论 0原文

我正在尝试使用 此触发器 以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-11-17 08:34:43

首先,我认为您的意思是

IF COALESCE(first_name, last_name, affiliation) IS NULL

因为 NOT NULL 意味着其中一个值(至少)不为空。

其次,@tofutim 是正确的;空字符串与 NULL 值不同。

First off, I think you mean

IF COALESCE(first_name, last_name, affiliation) IS NULL

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.

她说她爱他 2024-11-17 08:34:43

如果我理解你的逻辑不应该是这一行:

IF COALESCE(first_name, last_name, affiliation) IS NOT NULL THEN

Be

IF COALESCE(first_name, last_name, affiliation) IS NULL THEN

否则,只有当存在不为空的值时,你才会失败。

If I understand your logic shouldn't this line:

IF COALESCE(first_name, last_name, affiliation) IS NOT NULL THEN

Be

IF COALESCE(first_name, last_name, affiliation) IS NULL THEN

Otherwise, you'll fail only when there is a value that isn't null.

流殇 2024-11-17 08:34:43

触发器需要指定new.虚拟表,否则它将永远无法工作。
您可能需要考虑将触发器更改为。

CREATE TRIGGER check_not_all_null BEFORE INSERT ON agent FOR EACH ROW
BEGIN
   SET new.first_name = NULLIF(new.first_name, '');
   SET new.last_name = NULLIF(new.last_name, '');
   SET new.affiliation = NULLIF(new.affiliation , '');
   IF COALESCE(new.first_name, new.last_name, new.affiliation) IS NULL THEN
       CALL fail('All fields cannot be empty');
   END IF;
END $

这将强制将空字符串放入 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.

CREATE TRIGGER check_not_all_null BEFORE INSERT ON agent FOR EACH ROW
BEGIN
   SET new.first_name = NULLIF(new.first_name, '');
   SET new.last_name = NULLIF(new.last_name, '');
   SET new.affiliation = NULLIF(new.affiliation , '');
   IF COALESCE(new.first_name, new.last_name, new.affiliation) IS NULL THEN
       CALL fail('All fields cannot be empty');
   END IF;
END $

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

送你一个梦 2024-11-17 08:34:43

假设您的触发器根据已提供的优秀答案进行了修改,可以保证集合 {first_name, last_name, affiliation} 中的至少一个属性不为空。假设您还有一个 ID 号代理键,该表可能允许这样的数据。 (我不知道你所说的从属关系是什么意思,所以我只使用 char(1) 来实现。)

ID  first_name  last_name  affiliation
--
1   NULL        NULL       A
2   NULL        NULL       A
3   NULL        Nethery    B
4   Clare       NULL       C
5   Christian   Guilbert   A
6   Christian   Guilbert   A
7   Christian   Armistead  A
8   Hugh        Marchal    B
9   Sharron     Sevier     A
10  Christian   Guilbert   NULL

假设你的名字是“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.)

ID  first_name  last_name  affiliation
--
1   NULL        NULL       A
2   NULL        NULL       A
3   NULL        Nethery    B
4   Clare       NULL       C
5   Christian   Guilbert   A
6   Christian   Guilbert   A
7   Christian   Armistead  A
8   Hugh        Marchal    B
9   Sharron     Sevier     A
10  Christian   Guilbert   NULL

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文