2016-07-29 1 views
0

업데이트하지 않는 지금 내가 내 데이터베이스에이를 추가 할 내가 여기SQL 삽입 쿼리 삽입 여러 번 내가의 일부 데이터를 삽입 할

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 
          } 
         } 

        }); 
       } 

      } 

     }); 

을이를 보내 자바 스크립트 (코르도바 응용 프로그램)로 보낼 수있다 온라인 난 그냥 (내가 원하는)

productsnames를보고, 내가 PHP에서 쿼리하기 전에 $ 제품명 에코 언제 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 ."')"; 

과 같은 방법이있다 (BIND 변수는 나중에 할 것) 내 probl 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

참고이 방법 (또는 접근 INTO 교체하거나 심지어 ON DUPLICATE KEY 방식)이 필드에 고유 인덱스를 가질 필요가 성공적인 것으로에 그 너는 복제되기를 원하지 않는다.

교체

docs에서 상태는 테이블이 PRIMARY KEY 또는 UNIQUE 인덱스가있는 경우에만 의미가 있습니다. 그렇지 않으면 새 행이 다른 행을 복제하는지 여부를 판별하는 데 사용할 색인이 없기 때문에 INSERT와 동일하게됩니다. 아래 라인

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

는 JSON 후 모든 키 하나 하나의 * 수 injson 키 레코드 키가 삽입 배수를 반복하는 경우 때문에 루프가 몇 가지 문제가 몇 가지 문제를 가지고있는 것처럼

+0

REPLACE는이 경우 옵션이 아닙니다.이 케이스에 대한 조언은 무엇입니까? 존재하지 않을 경우 삽입하고 존재하는 경우 업데이트해야합니다. –

+0

삽입하기 전에 삽입 할 모든 값이있는 행을 선택하십시오. 그리고 리턴 행을 선택하면 그것을 갱신하고, 그렇지 않으면 삽입합니다. – Tamil

+0

나는 질의를했다. 그러나 그것은 작동하지 않는 것 같습니다. 쿼리가 정확하거나 다른 문제가 있습니다. 업데이트가 질문에 있습니다 –

0

보인다 시간 응답

res.data 로그를 제공하고 키에 무엇을 입력하고 루프를 반복하는지 확인하십시오.

+0

그래,하지만 그게 대답 이니, 나중에 설명하겠습니다. 내 문제는 이제 삽입입니다. 나가 respone 자료에 종사 할 때, 나는 당신이 말한 무슨을에 대해서 생각하고 해결책을 찾아 내십시오 찾아 낼 것이다 –