Php 如何插入mysql数组对象
我是Rest api的新手,我正在使用Rest api和angular js开发一个学生管理系统。当我从学生注册表注册学生时,有一个ng网格来选择学生将在学院学习的课程, 我在这样一个有角度的物体中加载了选定的课程Php 如何插入mysql数组对象,php,mysql,json,angularjs,rest,Php,Mysql,Json,Angularjs,Rest,我是Rest api的新手,我正在使用Rest api和angular js开发一个学生管理系统。当我从学生注册表注册学生时,有一个ng网格来选择学生将在学院学习的课程, 我在这样一个有角度的物体中加载了选定的课程 var Course = { "std_id": "1", "course_id": $scope.studentCourse, "started_date": $scope.startedDate }; std_id course
var Course = {
"std_id": "1",
"course_id": $scope.studentCourse,
"started_date": $scope.startedDate
};
std_id course_id started_date
- 1 , 2, 2015-9-18
- 1 , 4, 2015-9-18
- 1 , 8, 2015-9-18
在本课程中,对象课程是从ng grid plus student和registrations date中选择的值。我的问题是我已将对象加载到对象,并且我在restfull api中处理此对象时遇到了问题 我的json对象是这样传递的
[{
"std_id":"1",
"courses":
[{"course_id":"2"},
{"course_id":"4"},
{"course_id":"8"}],
"started_date":"2015-9-18"
}]
std_id course_id started_date
- 1 , 2, 2015-9-18
- 1 , 4, 2015-9-18
- 1 , 8, 2015-9-18
这里是我的php rest调用
private function InsertStudentCourse() {
if ($this->get_request_method() != "POST") {
$this->response('', 406);
}
$course= json_decode(file_get_contents("php://input"), true);
$column_names = array ('std_id','course_id','started_date');
$columns = '';
$values = '';
$keys = array_keys($course);
//$this->response($this->json($deviceTypeId), 200);
foreach ($column_names as $desired_key) { // Check the Department received. If blank insert blank into the array.
if (!in_array($desired_key, $keys)) {
$$desired_key = '';
} else {
$$desired_key = $course[$desired_key];
}
$columns = $columns . $desired_key . ',';
$values = $values . "'" . $$desired_key . "',";
}
$query = "INSERT INTO student_course (" . trim($columns, ',') . ") VALUES(" . trim($values, ',') . ")";
//$query = "INSERT INTO student_course ('course_id') VALUES ($c)";
if (!empty($course)) {
$r = $this->mysqli->query($query) or die($this->mysqli->error . __LINE__);
$success = array('status' => "Success", "msg" => "student Course Type Created Successfully.", "data" => $course);
$this->response($this->json($success), 200);
} else {
$this->response('', 204);
} //"No Content" status
}
我想像这样插入这个对象数组
std_id course_id started_date
- 1 , 2, 2015-9-18
- 1 , 4, 2015-9-18
- 1 , 8, 2015-9-18
标准课程开始日期 - 1 , 2, 2015-9-18 - 1 , 4, 2015-9-18 - 1 , 8, 2015-9-18
我如何才能做到这一点我强烈建议您一次只送一门课程。。。。并在创建或编辑时发送。看来你把团队组织在一个毫无意义的结构中,使事情变得过于复杂了。发送的数据是如何从
课程
的结构中改变其结构的?它几乎以json对象的形式传递学生的基本细节。它工作正常,我使用响应将std_id获取到下一个,作为课程注册[链接]()