暫無描述

insert_l.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. $server = "localhost";
  3. $user = "GroceryPolice";
  4. $passwd = "P5B64tTUmGljk7cB";
  5. $db = "GroceryPolice";
  6. //Opens a new connection to the MySQL server
  7. $con= mysqli_connect($server,$user, $passwd, $db);
  8. // Getting the received JSON into $Received_JSON variable.
  9. $Received_JSON = file_get_contents('php://input');
  10. // decoding the received JSON and store into $obj variable.
  11. $obj = json_decode($Received_JSON,true);
  12. // Populate User name from JSON $obj array and store into $user_name variable.
  13. $name_l = $obj['name_l'];
  14. // Populate User email from JSON $obj array and store into $user_email variable.
  15. $store = $obj['store'];
  16. // Populate Password from JSON $obj array and store into $user_password variable.
  17. $uid = $obj['uid'];
  18. //Checking User entered Email is already exist or not in MySQL database using SQL query.
  19. $CheckSQL = "SELECT * FROM `List` L WHERE L.uid=$uid and L.name_l='$name_l'";
  20. // Executing SQL Query.
  21. $check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
  22. // Executing SQL Query.
  23. if(empty($name_l) or empty($store) or empty($uid)){
  24. $MSG = 'Field is empty. Enter information again...' ;
  25. // Converting the message into JSON format.
  26. $json = json_encode($MSG);
  27. // Echo, Print the message on screen.
  28. echo $json;
  29. }
  30. else{
  31. if(isset($check)){
  32. $Duplicate_user = 'List Name Already Exist, Please Try Again';
  33. // Converting the message into JSON format.
  34. $Duplicate_user_Json = json_encode($Duplicate_user);
  35. // Echo, Printing the message on screen.
  36. echo $Duplicate_user_Json ;
  37. }
  38. else{
  39. // Creating SQL query and insert the record into MySQL database table if email dose not exist in database.
  40. $Sql_Query = "INSERT INTO `List`(`name_l`, `store`, `uid`) VALUES ('$name_l','$store','$uid')";
  41. if(mysqli_query($con,$Sql_Query)){
  42. // Show the success message.
  43. $MSG = 'List Registered Successfully' ;
  44. // Converting the message into JSON format.
  45. $json = json_encode($MSG);
  46. echo $json;
  47. }
  48. else{
  49. $MSG='Not Registered Correctly';
  50. $json = json_encode($MSG);
  51. echo $json;
  52. }
  53. }
  54. }
  55. mysqli_close($con);
  56. ?>