jquery - Insert specific values from json to database with php -
i have json data :
[ { "id_user":"31", "grade" : "a" }, { "id_user":"32", "grade" : "a" }, { "id_user":"33", "grade" : "b" }, { "id_user":"34", "grade" : "b" } ]
then send data jquery
$.post("myaction.php", {send: myjsondata }, function(res) { }, "json");
then in myaction.php, decode json , want send data database
$conn = mysqli_connect( "localhost","root","","mydb"); $data = json_decode($_post['send']); foreach($data $row){ $id_user = $row->id_user; $grade = $row->grade; mysqli_query($conn, "insert tbl_user (id_user,grade) values ('$id_user','$grade') "); }
but problem want insert id_user value equal grade a
, how can write query?
you can check grade's value in loop.
$grade = $row->grade; if($grade == 'a'){ //proceed here. }
Comments
Post a Comment