1

I have the following JSON.

{
    "studentIDPMKEY": "23",
    "Grades": [{
        "studentid": "23",
        "Maths": "C",
        "History": "B"
    }]
}

Now, I need to update this data into the SQL table.

But First, I need to do an SQL query to check if the student exists, then enter the grades. So I have written a function which checks if a student exists.

This is the Update statement

foreach($obj->student as $st){

$studentid= $st->studentid;
$maths=   $st->Maths;
$history=   $st->History;

$insert = $mysqli>prepare("UPDATE grades SET  $maths=?, $history=? WHERE studentid= ?");
$insert->bind_param("iss",$studentid,$maths, $history);
$insert->execute();
}

Now

What I want to do is, get the studentIDPMKEY and check if the student exist.

if (isset($_POST['studentIDPMKEY ']) && $_POST['v'] != "") {
}

My question is how can I loop through the above JSON string, first get the studentIDPMKEY , do the check than carrying on with update statement.

  • 2
    This is a separate issue, but your bind param arguments are in the wrong order, you want to pass maths, then history, then student id. `$insert->bind_param("ssi", $maths, $history, $studentid);` – Mark Jan 17 '20 at 10:39
  • https://stackoverflow.com/questions/4731242/php-loop-through-json-array and your binding should be like `$insert->bind_param("ssi",$maths, $history,$studentid);` – Amanjot Kaur Jan 17 '20 at 10:39
  • Can we see where `$obj->student` is set? – Mark Jan 17 '20 at 10:40
  • Does this answer your question? [php: loop through json array](https://stackoverflow.com/questions/4731242/php-loop-through-json-array) – Altherius Jan 17 '20 at 10:40
  • [`json_decode()`](https://www.php.net/manual/en/function.json-decode.php) – RiggsFolly Jan 17 '20 at 10:55

0 Answers0