Guest User

register.php

a guest
Feb 13th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. <?php
  2. #Accepting the data (parameters) passed from the application
  3. $username = $_POST["username"];
  4. $password = $_POST["password"];
  5. $email = $_POST["email"];
  6. $name = $_POST["name"];
  7. $mobile = $_POST["mobile"];
  8. $image = $_POST["image"];
  9.  
  10. #include the file which connects to the database
  11. include("dbConnect.php");
  12. #Array to be returned. (RESPONSE FROM SERVER)
  13. $response=array();
  14. $response["success"]=false;
  15.  
  16. $check=mysqli_query($conn,"SELECT * FROM `user_details` WHERE `username`='$username'");#checking for existing username
  17. $affected=mysqli_affected_rows($conn);#Number of rows selected
  18. if($affected>0){
  19.     #username already exist in the database
  20.     $response["status"]="USERNAME";
  21. }
  22. else{
  23.    
  24.     $result=mysqli_query($conn,"INSERT INTO `user_details` (`username`, `password`, `email`, `name`, `mobile`) VALUES ('$username', '$password', '$email', '$name', '$mobile')"); #CREATING A NEW RECORD IN DB
  25.    
  26.     #Fetching the id (Auto-Increment) assigned to the new record to use as the file name for profile picture.
  27.     $fetchId=mysqli_query($conn,"SELECT `id` FROM `user_details` WHERE `username`='$username'");
  28.     $id=0;
  29.     while($rowid=mysqli_fetch_assoc($fetchId)){
  30.         $id=$rowid['id'];
  31.  
  32.     }
  33.     $filePath = "dp/$id.png";#name of the file and the location where the image is to be updated10
  34.     #full URL of the file (used to download the image during login)
  35.     $url = "http://10.10.131.48/php/dp/$id.png";
  36.     $update=mysqli_query($conn,"UPDATE `user_details` SET `pic`='$url' WHERE `username`='$username'");#updating the profile-pic url
  37.     if($update){
  38.         file_put_contents($filePath,base64_decode($image));#storing the file after decoding from string format.
  39.         $response["success"]=true;#All operations gave been completed.
  40.     }
  41. }
  42. echo json_encode($response);#encoding RESPONSE into a JSON and returning.
  43. mysqli_close($conn);#closing the connection
  44. exit();
  45. ?>
Add Comment
Please, Sign In to add comment