我有一个困难,为什么在第二个表中插入数据不是"插入"在第一个表格中,它是"插入"。
controller.js
$scope.addStudent = function(student){
var data {
firstName: student.firstName,
gFirstName: student.firstName
};
$http.put("php/students/addStudent.php",data).then(function(response){
console.log(response);
});
}
addStudent.php
<?php
include("../../connection.php");
$data = json_decode(file_get_contents("php://input"));
$firstName = $data->firstName;
$gFirstName = $data->gFirstName;
$q = "INSERT INTO tblstudents (firstName) VALUES ('$firstName')";
$db->query($q);
$r = "INSERT INTO tblparents (firstName) VALUES ('$gFirstName')";
$db->query($r);
?>
我刚刚添加了$r query
,认为它会按照我预期的方式工作,但它没有按照我预期的方式工作。没有数据插入到表tblparents
中,而在表tblstudents
上正常插入数据。
connection.php
<?php
header("Cache-Control: no-cache, must-revalidate");
$db = new PDO("mysql:host=localhost;dbname=myDB","root","");
?>
答案 0 :(得分:0)
我确实尝试过@aynber在上面的问题评论中说的话。谢谢他。 还要感谢link of another related question.
现在我已更新 addStudent.php
<?php
include("../../connection.php");
$data = json_decode(file_get_contents("php://input"));
$firstName = $data->firstName;
$gFirstName = $data->gFirstName;
try {
$db->exec("SET CHARACTER SET utf8");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "
INSERT INTO `tblstudents`(`firstName`) VALUES (:firstName);
INSERT INTO `tblguardians`(`firstName`) VALUES (:gFirstName);
";
$statement = $db->prepare($sql);
$statement->bindValue(":firstName", $firstName);
$statement->bindValue(":gFirstName", $gFirstName);
$result = $statement->execute();
}
catch(PDOException $e) {
echo $e->getMessage();
}
echo json_encode($result);
?>