简体   繁体   English

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

[英]PHP/MySQL: how to prevent insertion of a row with all columns = NULL?

I'm trying to use this trigger in a PHP form.我正在尝试在 PHP 表单中使用此触发器 I've tried the following, but I still get null rows inserted into my table when I submit the form.我已经尝试了以下方法,但是当我提交表单时,我仍然会在我的表中插入 null 行。

$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.');

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.因为NOT NULL意味着其中一个值(至少)不是 null。

Second, @tofutim is correct;其次,@tofutim 是正确的; empty strings are not the same as NULL values.空字符串与NULL值不同。

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.否则,只有当值不是 null 时,您才会失败。

The trigger needs to have the new.触发器需要有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.这将强制将空字符串放入null中。

See: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_nullif请参阅: 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并且: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html

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.假设您的触发器(根据已提供的出色答案进行了修改)可确保集合 {first_name, last_name, affiliation} 的至少一个属性不是 null。 Assuming you also have a ID number surrogate key, that table might allow data like this.假设您还有一个 ID 号代理键,该表可能允许这样的数据。 (I don't know what you mean by affiliation, so I'll just use a char(1) for that.) (我不知道你所说的从属关系是什么意思,所以我只使用一个 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

Let's say that your name is "Christian Guilbert".假设你的名字是“Christian Guilbert”。 Only one of those rows (one of those surrogate ID number) is yours.这些行中只有一个(其中一个代理 ID 号)是您的。 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.我对数据库约束的第一条经验法则:dbms 允许的一切最终都会找到进入数据库的方式。 (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.) (我曾经看到一个财富 500 强的 IT 部门发布了一个应用程序,允许用户输入集装箱的负尺寸。你可以通过这种方式将很多箱子装进火车车厢。在纸上。)

IMHO, every one of those three columns should be declared NOT NULL.恕我直言,这三列中的每一列都应声明为 NOT NULL。 And I'm gonna upvote everyone else who says that, too.我也会投票给所有这么说的人。

(Names courtesy of The Random Name Generator .) (名称由随机名称生成器提供。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM