dongshen9058 2017-07-25 12:50
浏览 41
已采纳

使用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?

  • 写回答

1条回答 默认 最新

  • dongzhiji0814 2017-07-25 12:53
    关注

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

    $grade = $row->grade;
    if($grade == 'A'){
        //proceed here.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包