如果Record存在UPDATE MySql表,或者使用PHP进行INSERT

时间:2018-04-30 07:52:12

标签: php mysql

我必须检查表forex_rates_new中是否存在记录,我需要检查2个标准,它必须比较agency_id和currency_name。如果agency_id和currency_name相同,则系统必须UPDATE或INSERT。

forex_rates_new
id  agency_id   currency_name    rate
1   1111          aaa            1.898
2   2190          aaa            0.787

如果出现值为111的agency_id且出现带有USD的currency_name,则在数据库中。下次如果用户输入相同的值111和currency_name作为USD,则记录应进入更新模式或插入。这是我的问题:

INSERT INTO forex_rates_new (`agency_id`,`currency_name`,`rate`,`logo`,`created_at`) 
VALUES('".$agency_id."','".$currency_name."','".$rate."','".$logo."', now())

感谢任何帮助

5 个答案:

答案 0 :(得分:0)

我在这里给你简单的演示如何实现这一目标。首先,您需要选择数据。这样的事情。

"SELECT * FROM request WHERE agency_id='$agency_id' ";

现在检查数据是否存在make update query

"UPDATE request SET field name='$value' WHERE agency_id='$agency_id'";

否则就是这样的。

INSERT INTO forex_rates_new (`agency_id`,`currency_name`,`rate`,`logo`,`created_at`) 
VALUES('".$agency_id."','".$currency_name."','".$rate."','".$logo."', now())

您的代码应该是这样的。

$qry=mysqli_query($con,"SELECT * FROM request WHERE agency_id='$agency_id' ");
$rowCheck=mysqli_num_rows($qry);
    if ($rowCheck>0) { // if data exist update the data
        $qry=mysqli_query($con,"UPDATE request SET field name='$value' WHERE agency_id='$agency_id'");  
    }
    else{ // insert the data if data is not exist
        $qry=mysqli_query($con,"INSERT INTO request (field name) VALUES('$value')");
    }
  

记住给定的代码是对sql注入攻击开放的。这是逻辑   只有这不是完整的代码。避免使用sql注入攻击   准备好的陈述。

答案 1 :(得分:0)

  

truefalse

检查IFEXISTS
IF ( IF(EXISTS(SELECT `agency_id`,`currency_name` FROM forex_rates_new where `agency_id` = "$agency_id" AND  `currency_name` = "$currency_name"),1,0) ) THEN

BEGIN
    UPDATE forex_rates_new  
    SET
    `rate` = "$rate",
    `logo` = "$logo",
    `created_at` = now();
    WHERE `agency_id` = "$agency_id" AND `currency_name` = "$currency_name"
END;

ELSE

BEGIN
    INSERT INTO forex_rates_new 
    (`agency_id`,`currency_name`,`rate`,`logo`,`created_at`) 
    VALUES
    ("$agency_id","$currency_name","$rate","$logo", now());
END;

END IF;

答案 2 :(得分:0)

你走了!

<?php    
        $dupl = mysqli_query($con, "SELECT * FROM forex_rates_new WHERE currency_name='$agency_id'") or die(mysql_error($con));

        $row = mysqli_num_rows($dupl);
        if ($row > 0)
        {
            echo "<script language='javascript'>alert('Exists!!!')</script>";

        $stmt1 = $con->prepare("UPDATE forex_rates_new SET column_name=? WHERE column_name=?");
                        if($stmt1)
                        {
                            $stmt1->bind_param('is', $value, $value);
                            $stmt1->execute();
                        }
                      if($stmt1->affected_rows >0)
                      {
                        echo "<script language='javascript'>alert('Updated Sucessfully!!!')</script>";
                      }
                    else
                    {
                        echo "<script language='javascript'>alert('error!!!')</script>";
                    } 
        }

        $stmt = $con->prepare("INSERT into forex_rates_new VALUES(?,?,?,?, now())
        if($stmt)
        {
            $stmt->bind_param('isis', $currency_name, $agency_id, $rate, $logo);
            $stmt->execute();
        }
        if($stmt->affected_rows >0)
        {
            echo "<script language='javascript'>alert('Inserted Successfully!!!')</script>";
        }
        else{
            echo "<script language='javascript'>alert('An error occurred!!!')</script>";
        }
        } 
        ?>

以上是你想要的mysqli预备语句。

我代表整数 s代表字符串

答案 3 :(得分:0)

您可以将data () { return { name: '' } }, methods: { checkRequiredFields () { if(this.name == '') { alert("No value"); } } } 添加到UNIQUE KEYagency_id,然后您可以使用“REPLACE”语法 - &gt; https://dev.mysql.com/doc/refman/8.0/en/replace.html

无需为Update / Insert语句选择和添加条件。

答案 4 :(得分:-1)

INSERT INTO t1 (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html