0

I have issue that i am not able to retrieve values from the json array. Below is the code which i have written.

$receiver_data=$_POST['array'];

$new_array=json_decode($receiver_data,true);

foreach($new_array as $row)
{
    $m_id='".$row["m_id"]."'; 
    $u_id='".$row["u_id"]."';
    break;
}

$query = "DELETE FROM `squads` WHERE m_id=? AND u_id=? ";
$stmt = $con->prepare($query);
$stmt->bind_param("ss", $m_id,$u_id);
$stmt->execute();

$stmt->close();
  • In your loop `'".$row["m_id"]."'` with single quotes is a literal value (including the variable names,quotes etc.). Juts use `$row["m_id"]` – Nigel Ren Feb 05 '19 at 08:09

1 Answers1

4

You don't need quote for assignments
the data binging convert properly the type (ss)

$receiver_data=$_POST['array'];

$new_array=json_decode($receiver_data,true);

foreach($new_array as $row)
{
    $m_id= $row["m_id"] ; 
    $u_id=$row["u_id"];
    break;
}

$query = "DELETE FROM `squads` WHERE m_id=? AND u_id=? ";
$stmt = $con->prepare($query);
$stmt->bind_param("ss", $m_id,$u_id);
$stmt->execute();

$stmt->close();

and if you want delete all the rows remove break and add delete code in loop

$receiver_data=$_POST['array'];

$new_array=json_decode($receiver_data,true);

$query = "DELETE FROM `squads` WHERE m_id=? AND u_id=? ";
$stmt = $con->prepare($query);

foreach($new_array as $row)
{
    $m_id= $row["m_id"] ; 
    $u_id=$row["u_id"];
    $stmt->bind_param("ss", $m_id,$u_id);
    $stmt->execute();

}

$stmt->close();

or for avoid the loop and delete just the first

$receiver_data=$_POST['array'];

$new_array=json_decode($receiver_data,true);
$m_id= new_array[0]["m_id"] ; 
$u_id=new_array[0]["u_id"];

$query = "DELETE FROM `squads` WHERE m_id=? AND u_id=? ";
$stmt = $con->prepare($query);
$stmt->bind_param("ss", $m_id,$u_id);
$stmt->execute();

$stmt->close();
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107