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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -