Guest User

Untitled

a guest
Feb 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. <?php
  2.  
  3. $response['success'] = array('success' => false, 'messages' => array());
  4.  
  5. $Id = trim(intval($_POST['id']));
  6. $name = strip_tags(trim(strval($_POST['name'])));
  7. $age = strip_tags(trim(strval($_POST['age'])));
  8. $gender = strip_tags(trim(strval($_POST['gender'])));
  9.  
  10. $data = array();
  11. if(count($_POST['data']) > 0){
  12. foreach($_POST['data'] as $key => $array){
  13. $row = array();
  14. $row['subject_id'] = intval($array['subject_id']);
  15. $data[$key] = $row;
  16. }
  17. }
  18. try{
  19.  
  20. $query1 = "insert into students (name,age,gender) values(:name,:age,:gender)";
  21.  
  22. $query1 = $con->prepare($query1);
  23. $query1->bindparam(':name',$p_name);
  24. $query1->bindparam(':age',$age);
  25. $query1->bindparam(':gender',$gender);
  26. $run_query1 = $query1->execute();
  27.  
  28.  
  29. foreach($data as $key => $array){
  30. $check_multiple = $db->prepare("SELECT * From subjects WHERE subject_id = :subject_id ");
  31. $check_multiple->execute(array(':subject_id' => $array['subject_id']));
  32.  
  33. $count = $check_multiple->rowCount();
  34. if($count > 0) {
  35. while($row1 = $check_multiple->fetch(PDO:: FETCH_ASSOC) ) {
  36. $subject_id = $row1['subject_id'];
  37. $subject = $row1['subject'];
  38. $part_name = $row1['part'];
  39.  
  40.  
  41. $query2 = $db->prepare("INSERT INTO results (student_id,subject_id,subject,part) values(:student_id,:subject_id,:subject,:part)");
  42.  
  43. $query2->bindparam(':student_id', $student_id);
  44. $query2->bindparam(':subject_id', $subject_id);
  45. $query2->bindparam(':subject', $subject);
  46. $query2->bindparam(':part', $part);
  47. $query2->execute();
  48. }
  49. }
  50. }
  51. if (!$run_query1 && !$query2){
  52. $result['success'] = false;
  53. $result['messages'] = "Error";
  54. } else {
  55. $result['success'] = true;
  56. $result['messages'] = "Success";
  57. }
  58. }
  59. catch(PDOException $e){
  60. echo 'Connection failed: ' . $e->getMessage();
  61. }
  62.  
  63. echo json_encode($result);
  64. ?>
Add Comment
Please, Sign In to add comment