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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看