drh19790711 2016-02-13 22:41
浏览 128

使用PHP将JSON数组添加到MySQL数据库

I have a AngularJS Function that sends an array to my PHP. But how do I go through this array in PHP and insert each value into MySQL database. SoI have the following $http post:

var data = {
  "username": $scope.un,
  "courses": $scope.selectedCourses
}

$http({
url: "submitCourses.php",
method: "POST",
 data: data
 }).success(function(response) {
   $scope.courseSubmitResponse = response.resp;

 });

And the $scope.selectedCourses looks like ["COEN 10", "MATH 54", "HIST 99"] when sent over. So how could I insert each of these values into my database? Is there a way I can assign a php variable to the array, then do a for loop of that php variable? Your help is greatly appreciated.

  • 写回答

3条回答 默认 最新

  • dongpiao1983 2016-02-13 23:03
    关注

    I am assuming data transmitted from client side to server side is json.

    below is the snippet:

    $data = json_decode($data, true);
    
    foreach($data as $element) {
        list($subject, $mark) = explode(" ", $element);
        // insert using PDO into the database.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)