Guest User

Untitled

a guest
Sep 25th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <?php
  2. $servername = "localhost";
  3. $username = "root";
  4. $password = "";
  5. $database = "life_saver";
  6. //creating a new connection object using mysqli
  7. $conn = new mysqli($servername, $username, $password, $database);
  8. //an array to display response
  9. $response = array();
  10. //displaying the response in json structure
  11. # Get JSON as a string
  12. $json_str = file_get_contents('php://input');
  13. # Get as an object
  14. $json_obj = json_decode($json_str);
  15. if(isTheseParametersAvailable
  16. (array('id','role_id','Designation','Department','Father_name','Section','CNIC','ICE_No','Postal_Address')))
  17. {
  18. //getting the values
  19. $id = $json_obj->id;
  20. $role_id = $json_obj->role_id;
  21. $Designation = $json_obj->Designation;
  22. $Department = $json_obj->Department;
  23. $Father_name = $json_obj->Father_name;
  24. $Section = $json_obj->Section;
  25. $CNIC = $json_obj->CNIC;
  26. $ICE_No = $json_obj->ICE_No;
  27. $Postal_Address = $json_obj->Postal_Address;
  28. //if user is new creating an insert query
  29. $stmt = $conn->prepare("UPDATE users ( role_id, Designation,Department, Father_name,Section,CNIC,ICE_No,Postal_Address ) VALUES (?, ?, ?, ?,?,?,?,? ) WHERE id=? ");
  30.  
  31. $stmt->bind_param("ssssssss", $role_id, $Designation,$Department, $Father_name,$Section,$CNIC,$ICE_No,$Postal_Address);
  32. //if the user is successfully added to the database
  33. if($stmt->execute()){
  34. //fetching the user back
  35. $stmt = $conn->prepare("SELECT id, id, role_Id, Designation,Department, Father_name,Section,CNIC,ICE_No,Postal_Address FROM users WHERE id = ?");
  36. $stmt->bind_param("s",$id);
  37. $stmt->execute();
  38. $stmt->bind_result($userid, $id, $role_id, $Designation,$Department, $Father_name,$Section,$CNIC,$ICE_No,$Postal_Address);
  39. $stmt->fetch();
  40. $user = array(
  41. 'id'=>$id,
  42. 'role_id'=>$role_id,
  43. 'Designation'=>$Designation,
  44. 'Department'=>$Department,
  45. 'Father_name'=>$Father_name,
  46. 'Section'=>$Section,
  47. 'CNIC'=>$CNIC,
  48. 'ICE_No'=>$ICE_No,
  49. 'Postal_Address'=>$Postal_Address
  50. );
  51. $stmt->close();
  52. //adding the user data in response
  53. $response['message'] = 'User Updated successfully';
  54. $response['user'] = $user;
  55. }
  56. }
  57. else{
  58. $response['message'] = 'required parameters are not available';
  59. }
  60. echo json_encode($response);
  61. //function validating all the paramters are available
  62. //we will pass the required parameters to this function
  63. function isTheseParametersAvailable($params){
  64. # Get JSON as a string
  65. $json_str = file_get_contents('php://input');
  66. # Get as an object
  67. $json_obj = json_decode($json_str);
  68. //traversing through all the parameters
  69. foreach($params as $param){
  70. //if the paramter is not available
  71. if(!$json_obj->$param){
  72. //return false
  73. return false;
  74. }
  75. }
  76. //return true if every param is available
  77. return true;
  78. }
Add Comment
Please, Sign In to add comment