2016-07-29 123 views
0

我要插入一些数据,是与JavaScript(科尔多瓦的应用程序)发送我送这个位置SQL INSERT查询插入多次,不更新

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查询之前呼应$产品名称,我只看到productsnames(我想)

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

有人可以帮助我吗?在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); 


     } 
+1

如果我是正确的,'userID'是'card'表的主键,并且你没有将'userID'传递给'REPLACE'查询。 – antorqs

+0

对不起,我的坏,我还有工作的答复呢,它只是关于数据库在线... –

回答

0

请注意,这种方法(或REPLACE INTO的办法,甚至对重复键的方法)是全成你需要有领域的唯一索引你不想重复。

docs状态

更换使得只有当一个表有一个主键或唯一索引的意义。否则,它就等同于INSERT,因为没有用于确定新行是否与其他行重复的索引。

+0

所以REPLACE在这种情况下不是一个选项,你对这种情况的建议是什么。如果不存在,则需要插入并更新(如果存在) –

+0

在执行插入操作之前,选择要插入所有值的行。如果选择返回行,那么你会更新它,否则插入。 – Tamil

+0

我作了一个查询。但它似乎不工作。查询是否正确或是否存在其他问题,更新是在问题 –

0

看起来你有以下线

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

这个循环有一定的问题,因为如果它的JSON然后遍历所有关键的一招一式按键记录按键的它插入多个injson *数一些问题时间响应

请提供res.data日志,并检查您在密钥中获取的内容以及循环该循环的次数。

+0

噢,是的,但这是答复,我会在稍后做出。我现在的问题是插入。当我处理回复数据时,我会考虑你说的话并找到解决办法 –