使用php将特定值从json插入数据库

使用php将特定值从json插入数据库

问题描述:

i have json data like this :

[

{
"id_user":"31",
"grade" : "A"
},

{
"id_user":"32",
"grade" : "A"
},

{
"id_user":"33",
"grade" : "B"
},

{
"id_user":"34",
"grade" : "B"
}

]

then i send the data with jquery

$.post("myaction.php",
{send: myjsondata }, function(res) {


 }, "json");

then in myaction.php, i decode the json and i want to send the data to database

$conn = mysqli_connect( "localhost","root","","mydb");

$data = json_decode($_POST['send']);

foreach($data as $row){

    $id_user = $row->id_user;
    $grade = $row->grade;
    mysqli_query($conn, "INSERT INTO tbl_user 
(id_user,grade) VALUES ('$id_user','$grade') ");
}

but the problem is i want insert with id_user where the value is equal to grade A, how can i write the query?

You can check the grade's value in the for loop.

$grade = $row->grade;
if($grade == 'A'){
    //proceed here.
}