简体   繁体   English

PHP MYSQL更新时如何检查记录是否已存在?

[英]PHP MYSQL How to check if record already exists when UPDATING?

For my employee's table, an e-mail is a unique field. 对于我的员工的桌子,电子邮件是唯一的字段。 When I insert a new employee, I check on the existance of the email. 插入新员工时,我检查电子邮件的存在。 If it already exists, you get an error message: 如果已经存在,则会收到错误消息:

    $selectquery ="SELECT * FROM employee WHERE empemail='$empemail'";
    $selectresult=mysql_query($selectquery) or die("query fout " . mysql_error() );
    if(mysql_num_rows($selectresult)==1)
    {
        $errormsgnewemployee = '<p id=notification>The email you entered already exists.</p>';
    }

But how to do it when UPDATING a field? 但是,更新字段时该怎么做? Because if I update an employees data (without changing his mail), it will not let me, because the email already exists. 因为如果我更新了一个员工数据(不更改他的邮件),它不会放过我,因为该电子邮件已经存在。 All I can think of it is to UPDATE first, check then if there are 2 records, if there are 2, update it back as it was before. 我能想到的就是先进行UPDATE,然后检查是否有2条记录,如果有2条记录,请像以前一样将其更新回来。

Is this a good approach or is there a better solution? 这是一个好方法还是有更好的解决方案?

EDIT 编辑

My own solution is to UPDATE the table having a unique index on the table and using the query error: 我自己的解决方案是更新具有unique index的表,并使用查询错误:

    if (mysql_error())
            {
                $errormsgnewemployee = '<p id=notification>Update failed. Please check if the email you entered already exists.</p>';
            }

Because, why else would the user have error if it's not for the only unique field? 因为,如果不是唯一字段,用户为什么还会出错? In all other cases the update would succeed. 在所有其他情况下,更新将成功。

I would recommend you to use INSERT ... ON DUPLICATE UPDATE ... sql construction. 我建议您使用INSERT ... ON DUPLICATE UPDATE ... sql构造。 Take a deeper look at manual . 深入了解手册

So your query could be the following: 因此,您的查询可能是以下内容:

INSERT INTO employee (email, first_name, last_name, ...) 
VALUES ('employee_email@example.com', 'nik', 'smtih', ...) 
ON DUPLICATE UPDATE 
    first_name = 'nik', 
    last_name = 'smith', 
    ...

Pay attention that INSERT part of this query will fail due to UNIQUE KEY constraint, that is why the second part of the query, ON DUPLICATE UPDATE , will be executed and the data will be updated instead of inserting. 请注意,由于UNIQUE KEY约束,此查询的INSERT部分将失败,这就是为什么查询的第二部分ON DUPLICATE UPDATE将被执行,并且数据将被更新而不是插入。

You can use the same solution, you just need to "fetch this email address, but not mine" to check if there are any other users using it. 您可以使用相同的解决方案,只需“获取此电子邮件地址,而不是我的”,以检查是否还有其他用户在使用它。

Eg. 例如。 Before the update, do something like this: 在更新之前,请执行以下操作:

SELECT 1 FROM employee WHERE empemail = '$empemail' AND id != '$myid';

Should only return rows where the email belongs to someone else. 仅应返回电子邮件所属的行。

On a side note, there are potential security issues with interpolating variables into SQL queries like that - you shouldn't do it. 附带说明一下,这样将变量插值到SQL查询中可能会带来安全问题-您不应该这样做。 And the mysql_* functions are being deprecated should be replaced with PDO or mysqli alternatives. 并且不推荐使用mysql_ *函数,应将其替换为PDO或mysqli替代方案。

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

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