SQL - IF EXISTS UPDATE ELSE INSERT INTO

时间:2023-04-29
本文介绍了SQL - IF EXISTS UPDATE ELSE INSERT INTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想要做的是在我的数据库中 INSERT 订阅者,但是 IF EXISTS 它应该 UPDATE 行,ELSE INSERT INTO 新行.

当然,我首先连接到数据库,然后从 GET 获取 $name$email$birthday网址字符串.

$con=mysqli_connect("localhost","---","---","---");//检查连接如果 (mysqli_connect_errno()){echo "连接 MySQL 失败:".mysqli_connect_error();}$name=$_GET['name'];$email=$_GET['email'];$birthday=$_GET['生日'];

这有效,但只是添加了新行;

mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)值 ('$name', '$email', '$birthday')");mysqli_close($con);

这是我尝试过的;

mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)值 '$name', '$email', '$birthday'ON DUPLICATE KEY UPDATE subs_name = VALUES($name), subs_birthday = VALUES($birthday)");mysqli_close($con);

mysqli_query($con,"IF EXISTS (SELECT * FROM subs WHERE subs_email='$email')更新 subs SET subs_name='$name', subs_birthday='$birthday' WHERE subs_email='$email'别的INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES ('$name', '$email', '$birthday')");mysqli_close($con);

mysqli_query($con,"IF NOT EXISTS(SELECT * FROM subs WHERE subs_email='$email')开始INSERT INTO subs (subs_name, subs_email, subs_birthday)值 ('$name', '$email', '$birthday')结尾");mysqli_close($con);

但它们都不起作用,我做错了什么?

非常感谢任何帮助!

解决方案

  1. 创建UNIQUE 约束 在您的 subs_email 列上,如果尚不存在:

    ALTER TABLE subs ADD UNIQUE (subs_email)

  2. 使用 INSERT ... ON DUPLICATE KEY更新:

    INSERT INTO subs(subs_name, subs_email, subs_birthday)价值观(?,?,?)关于重复密钥更新subs_name = VALUES(subs_name),subs_birthday = VALUES(subs_birthday)

<块引用>

您可以在 UPDATE 子句中使用 VALUES(col_name) 函数来从 INSERT ... ON 的 INSERT 部分引用列值重复密钥更新 - dev.mysql.com

  1. 请注意,我使用参数占位符代替字符串文字,因为真的应该使用参数化语句来防御 SQL 注入攻击.

What I'm trying to do is INSERT subscribers in my database, but IF EXISTS it should UPDATE the row, ELSE INSERT INTO a new row.

Ofcourse I connect to the database first and GET the $name, $email and $birthday from the url string.

$con=mysqli_connect("localhost","---","---","---");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$name=$_GET['name']; 
$email=$_GET['email'];
$birthday=$_GET['birthday'];

This works, but just adds the new row;

mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)
VALUES ('$name', '$email', '$birthday')");

mysqli_close($con);

Here's what I tried;

mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)
VALUES '$name', '$email', '$birthday'
ON DUPLICATE KEY UPDATE subs_name = VALUES($name), subs_birthday = VALUES($birthday)");
mysqli_close($con);

and

mysqli_query($con,"IF EXISTS (SELECT * FROM subs WHERE subs_email='$email')
    UPDATE subs SET subs_name='$name', subs_birthday='$birthday' WHERE subs_email='$email'
ELSE
    INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES ('$name', '$email', '$birthday')");
mysqli_close($con);

and

mysqli_query($con,"IF NOT EXISTS(SELECT * FROM subs WHERE subs_email='$email')
Begin
INSERT INTO subs (subs_name, subs_email, subs_birthday)
VALUES ('$name', '$email', '$birthday')
End");
mysqli_close($con);

But none of them work, what am I doing wrong?

Any help is greatly appreciated!

解决方案

  1. Create a UNIQUE constraint on your subs_email column, if one does not already exist:

    ALTER TABLE subs ADD UNIQUE (subs_email)
    

  2. Use INSERT ... ON DUPLICATE KEY UPDATE:

    INSERT INTO subs
      (subs_name, subs_email, subs_birthday)
    VALUES
      (?, ?, ?)
    ON DUPLICATE KEY UPDATE
      subs_name     = VALUES(subs_name),
      subs_birthday = VALUES(subs_birthday)
    

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE - dev.mysql.com

  1. Note that I have used parameter placeholders in the place of string literals, as one really should be using parameterised statements to defend against SQL injection attacks.

这篇关于SQL - IF EXISTS UPDATE ELSE INSERT INTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:没有双重查询的 MySQL 分页? 下一篇:在 MySQL 中将当前时间添加 2 小时?

相关文章