【问题标题】:SQL insert query insert multiple times and don't updateSQL插入查询插入多次并且不更新
【发布时间】:2016-12-04 01:12:20
【问题描述】:

我想插入一些通过 javascript(cordova 应用程序)发送的数据,我在这里发送

var link = 'somelink';
        var stmt = "SELECT * FROM card WHERE Productsync = 0 ";
        //console.log(stmt);
        $cordovaSQLite.execute(db, stmt).then(function(res) { //console.log(res)
            if(res.rows.length > 0) {
                for (var i = 0; i < res.rows.length; i++){
                    console.log(res.rows.item(i));
                    console.log(res.rows.item(i).ProductName);
                    console.log(res.rows.item(i).ProductBarcode);
                    console.log(res.rows.item(i).ProductPakking);
                    console.log(res.rows.item(i).ProductPresent);
                    console.log(res.rows.item(i).ProductMinimuim);
                    console.log(res.rows.item(i).FotoUrl);
                    console.log(res.rows.item(i).DBid);





                    $http.post(link, {ProductName : res.rows.item(i).ProductName , ProductBarcode : res.rows.item(i).ProductBarcode , ProductPakking : res.rows.item(i).ProductPakking , ProductPresent : res.rows.item(i).ProductPresent , ProductMinimuim : res.rows.item(i).ProductMinimuim , FotoUrl : res.rows.item(i).FotoUrl , DBid : res.rows.item(i).DBid}).then(function (res){//sending data
                        console.log(res.data);//respone of server
                        var data = res.data;
                        for (var key in data) {//read out respone
                            if (data.hasOwnProperty(key)) {
                                var obj = data[key];
                                console.log(obj);
                                //have to make this
                            }
                        }

                    });
                }

            }

        });

现在我希望它在线将它添加到我的数据库中(我稍后会做 BIND 变量)这就是我的 php 脚本的样子

    $postdata = file_get_contents("php://input");
if (isset($postdata)) {
    $request = json_decode($postdata);

    $ProductName = $request->ProductName;
    $ProductBarcode = $request->ProductBarcode;
    $ProductPakking = $request->ProductPakking;
    $ProductPresent = $request->ProductPresent;
    $ProductMinimuim = $request->ProductMinimuim;
    $FotoUrl = $request->FotoUrl;
    $DBid = $request->DBid;



    $ProductName = mysqli_real_escape_string($connection,$ProductName);
    $ProductBarcode = mysqli_real_escape_string($connection,$ProductBarcode);
    $ProductPakking = mysqli_real_escape_string($connection,$ProductPakking);
    $ProductPresent = mysqli_real_escape_string($connection,$ProductPresent);
    $ProductMinimuim = mysqli_real_escape_string($connection,$ProductMinimuim);
    $FotoUrl = mysqli_real_escape_string($connection,$FotoUrl);
    $DBid = mysqli_real_escape_string($connection,$DBid); 
     $query ="REPLACE into `card` (ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
        VALUES( '" . $ProductName . "' ,
                '" . $ProductBarcode . "' ,
                '" . $ProductPakking . "' ,
                '" . $ProductPresent . "' ,
                '" . $ProductMinimuim . "' ,
                '" . $FotoUrl . "' ,
                '" . $DBid ."')";

当我在 php 查询之前回显 $ProductName 时,我只看到产品名称(我想要的)

我的问题是:如果该值已经存在,它不会替换该值,并且它插入多次,如果该列已经存在,它必须只插入一次并替换或更新。

有人可以帮我吗?

在 PHP 中更新查询

            $query ="SELECT ProductName FROM `card` WHERE
                `ProductName` = '" . $ProductName . "' AND
                `ProductBarcode` = '" . $ProductBarcode . "' AND
                `ProductPakking` = '" . $ProductPakking . "' AND
                `ProductPresent` = '" . $ProductPresent . "' AND
                `ProductMinimuim` = '" . $ProductMinimuim . "' AND
                `FotoUrl` = '" . $FotoUrl . "' AND
                `DBid` = '" . $DBid ."'";
        $result_set = mysqli_query($connection,$query);
        if (mysqli_num_rows($result_set) == 0){
            $query ="INSERT INTO `card` (ProductName,ProductBarcode,ProductPakking,ProductPresent,ProductMinimuim,FotoUrl,DBid)
            VALUES( '" . $ProductName . "' ,
                    '" . $ProductBarcode . "' ,
                    '" . $ProductPakking . "' ,
                    '" . $ProductPresent . "' ,
                    '" . $ProductMinimuim . "' ,
                    '" . $FotoUrl . "' ,
                    '" . $DBid ."')";
            $result = mysqli_query($connection,$query);

        }else{

            $query = "UPDATE `card`
            SET  `ProductName` = '" . $ProductName . "' ,
                `ProductBarcode` = '" . $ProductBarcode . "' ,
                `ProductPakking` = '" . $ProductPakking . "' ,
                `ProductPresent` = '" . $ProductPresent . "' ,
                `ProductMinimuim` = '" . $ProductMinimuim . "' ,
                `FotoUrl` = '" . $FotoUrl . "' ,
                `DBid` = '" . $DBid ."'
            WHERE `ProductBarcode` = '" . $ProductBarcode . "' AND `DBid` = '" . $DBid ."'";

            $fines = mysqli_query($connection,$query);


        }

【问题讨论】:

  • 如果我是正确的,userIDcard 表的主键,并且您没有将 userID 传递给 REPLACE 查询。
  • 对不起,我的错,我已经处理了响应,它只是关于在线数据库......

标签: javascript php mysql cordova insert-update


【解决方案1】:

请注意,要使这种方法(或 REPLACE INTO 方法甚至 ON DUPLICATE KEY 方法)成功,您需要在不希望重复的字段上具有唯一索引。

来自docs

仅当表具有 PRIMARY KEY 或 UNIQUE 索引时,REPLACE 才有意义。否则,它就等同于 INSERT,因为没有索引可用于确定新行是否与另一行重复。

【讨论】:

  • 所以在这种情况下,REPLACE 不是一个选项,你对这种情况有什么建议。如果不存在我需要插入,如果存在则更新
  • 在插入之前,选择要插入所有值的行。如果选择返回行,那么你会更新它,否则插入。
  • 我做了一个查询。但这似乎不起作用。查询正确还是有其他问题,更新在问题中
  • 您的更新查询有错误。它应该是更新 card SET productName = , ProductBarcode = , ... 它不应该与 AND 连接
  • 好的,我修复了错误,但它仍然插入多次
【解决方案2】:

看起来你对下面的行有一些问题

console.log(res.data);//respone of server
var data = res.data;
for (var key in data) {

这个循环有一些问题,因为如果它是json,那么它会一个一个地遍历所有键,并在json中插入多个键记录键*时间响应数

请提供 res.data 日志,并检查您在密钥中获得了什么以及您在该循环中循环了多少次。

【讨论】:

  • ooo 是的,但这就是答案,我稍后会做这个。我现在的问题是插入。当我处理响应数据时,我会考虑您所说的并找到解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
相关资源
最近更新 更多