2017-05-19 11 views
0

これは2つの操作、1つは配列に項目を追加してMySQLでその配列を更新し、2つ目は同じ項目を削除するPHPコードですMySQLデータベースの更新と更新を行います。 いくつかのクエリを正しく実行し、データベース内の完全な形式で配列を更新します。配列は["2"、 "9"、 "4"、 "3"、 "6"]のようになります。クエリの後には、配列のフォーマットが変更され、次のようになります。{"0": "2"、 "2": "4"、 "3": "3"、 "4": "6"}。 (この場合、配列から2番目の要素を削除しました)。なぜこれが起こるのですか?助けて !!PHPからのクエリの後にMySQLデータベースのエントリが変更される

PHPコード:::

<?php 
$con=mysqli_connect("xxx","yyy","zzz","xyz"); 
if($_SERVER['REQUEST_METHOD']=='GET'){  
    $todo = (isset($_GET['todo'])) ? intval($_GET['todo']) : 0; 
    $user_id = (isset($_GET['user_id'])) ? intval($_GET['user_id']) : 0; 
    $product_id = (isset($_GET['product_id'])) ? $_GET['product_id'] : "0"; 
} else { 
    echo 'Error'; 
} 
// When the value of $todo is 1, it means Add, and when it is 2, it means remove 
if($todo == 1){ //add 
    $q="SELECT wishlist FROM user where user_id = '".$user_id."'"; 
    $query = mysqli_query($con, $q); 
    $row = mysqli_fetch_array($query); 
    if (mysqli_num_rows($query) > 0) { 
     $a = '"' . $product_id . '"'; 
     if(strpos($row['wishlist'], $a) !== false){ //matched 
      $response["error"] = true; 
      $response["message"] = 'Product already in Wishlist'; 
      die(json_encode($response)); 
     } else { 
      $wishlist = array(); 
      $wishlist = json_decode($row['wishlist'], true); 
      array_push($wishlist, $product_id); 
      $q1="UPDATE `user` SET `wishlist` = '".json_encode($wishlist)."' WHERE `user_id` = '".$user_id."'"; 
      $query1 = mysqli_query($con, $q1);   
      $response["error"] = false; 
      $response["message"] = 'Product added to Wishlist'; 
      die(json_encode($response)); 
     } 
    } else { 
     $response["error"] = true; 
     $response["message"] = 'Error 1'; 
     die(json_encode($response)); 
    } 
} else if($todo == 2){ //remove 
    $q2="SELECT wishlist FROM user where user_id = '".$user_id."'"; 
    $query2 = mysqli_query($con, $q2); 
    $row2 = mysqli_fetch_array($query2); 
    if (mysqli_num_rows($query2) > 0) { 
     $a1 = '"' . $product_id . '"'; 
     if(strpos($row2['wishlist'], $a1) !== false){ //matched 
      $wishlist1 = array(); 
      $wishlist1 = json_decode($row2['wishlist'], true); 
      if(($key = array_search($product_id, $wishlist1)) !== false) { 
       unset($wishlist1[$key]); 
      } 
      $q3="UPDATE `user` SET `wishlist` = '".json_encode($wishlist1)."' WHERE `user_id` = '".$user_id."'"; 
      $query3 = mysqli_query($con, $q3);   
      $response["error"] = false; 
      $response["message"] = 'Product removed from Wishlist'; 
      die(json_encode($response)); 
     } else { 
      $response["error"] = true; 
      $response["message"] = 'Product does not exist in Wishlist'; 
      die(json_encode($response)); 
     } 
    } 
} 
else { 
    $response["error"] = true; 
    $response["message"] = 'Error 3'; 
    die(json_encode($response));  
} 
mysqli_close($con); 
?> 

答えて

0

unsetは、数値配列インデックスの「穴」を残す - ので、それがあるので、それは、あなたがJSONとしてエンコードオブジェクトに変換されますこの場合、実際のキー値を保持するための唯一の方法です。

数値配列を連続的に取得するには、array_valuesを使用してから、JSONとしてエンコードしてください。

+0

ありがとうございます。出来た –

関連する問題