Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "";
- $database = "life_saver";
- //creating a new connection object using mysqli
- $conn = new mysqli($servername, $username, $password, $database);
- //an array to display response
- $response = array();
- //displaying the response in json structure
- # Get JSON as a string
- $json_str = file_get_contents('php://input');
- # Get as an object
- $json_obj = json_decode($json_str);
- if(isTheseParametersAvailable
- (array('id','role_id','Designation','Department','Father_name','Section','CNIC','ICE_No','Postal_Address')))
- {
- //getting the values
- $id = $json_obj->id;
- $role_id = $json_obj->role_id;
- $Designation = $json_obj->Designation;
- $Department = $json_obj->Department;
- $Father_name = $json_obj->Father_name;
- $Section = $json_obj->Section;
- $CNIC = $json_obj->CNIC;
- $ICE_No = $json_obj->ICE_No;
- $Postal_Address = $json_obj->Postal_Address;
- //if user is new creating an insert query
- $stmt = $conn->prepare("UPDATE users ( role_id, Designation,Department, Father_name,Section,CNIC,ICE_No,Postal_Address ) VALUES (?, ?, ?, ?,?,?,?,? ) WHERE id=? ");
- $stmt->bind_param("ssssssss", $role_id, $Designation,$Department, $Father_name,$Section,$CNIC,$ICE_No,$Postal_Address);
- //if the user is successfully added to the database
- if($stmt->execute()){
- //fetching the user back
- $stmt = $conn->prepare("SELECT id, id, role_Id, Designation,Department, Father_name,Section,CNIC,ICE_No,Postal_Address FROM users WHERE id = ?");
- $stmt->bind_param("s",$id);
- $stmt->execute();
- $stmt->bind_result($userid, $id, $role_id, $Designation,$Department, $Father_name,$Section,$CNIC,$ICE_No,$Postal_Address);
- $stmt->fetch();
- $user = array(
- 'id'=>$id,
- 'role_id'=>$role_id,
- 'Designation'=>$Designation,
- 'Department'=>$Department,
- 'Father_name'=>$Father_name,
- 'Section'=>$Section,
- 'CNIC'=>$CNIC,
- 'ICE_No'=>$ICE_No,
- 'Postal_Address'=>$Postal_Address
- );
- $stmt->close();
- //adding the user data in response
- $response['message'] = 'User Updated successfully';
- $response['user'] = $user;
- }
- }
- else{
- $response['message'] = 'required parameters are not available';
- }
- echo json_encode($response);
- //function validating all the paramters are available
- //we will pass the required parameters to this function
- function isTheseParametersAvailable($params){
- # Get JSON as a string
- $json_str = file_get_contents('php://input');
- # Get as an object
- $json_obj = json_decode($json_str);
- //traversing through all the parameters
- foreach($params as $param){
- //if the paramter is not available
- if(!$json_obj->$param){
- //return false
- return false;
- }
- }
- //return true if every param is available
- return true;
- }
Add Comment
Please, Sign In to add comment