mysql - AngularJs + PHP Inserting Data work in first table but not in second -
i having difficulty of why inserting data in second table not "inserting" when in first table "inserted".
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 tblstudents (firstname) values ('$firstname')"; $db->query($q); $r = "insert tblparents (firstname) values ('$gfirstname')"; $db->query($r); ?>
i added $r query
thinking work way expected work it's not working way expected. no data being inserted table tblparents
, while on table tblstudents
data inserted normally.
connection.php
<?php header("cache-control: no-cache, must-revalidate"); $db = new pdo("mysql:host=localhost;dbname=mydb","root",""); ?>
i did try @aynber said in comment in question above. him. , link of related question.
this updated 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 `tblstudents`(`firstname`) values (:firstname); insert `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); ?>
Comments
Post a Comment