No Description

insert_productsss.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. #Connecting to the GroceryPolice Database
  3. $server = "localhost";
  4. $user = "GroceryPolice";
  5. $passwd = "P5B64tTUmGljk7cB";
  6. $db = "GroceryPolice";
  7. //Opens a new connection to the MySQL server
  8. $con= mysqli_connect($server,$user, $passwd, $db);
  9. //Receives JSON file from the app
  10. // Getting the received JSON into $Received_JSON variable.
  11. $Received_JSON = file_get_contents('php://input');
  12. // decoding the received JSON and store into $obj variable.
  13. $obj = json_decode($Received_JSON,true);
  14. // Populate from JSON $obj array and store into $user_name variable.
  15. $name_p=$obj['name_p'];
  16. $bought_date=$obj['bought_date'];
  17. $expiration_date=$obj['expiration_date'];
  18. $type=$obj['type'];
  19. $lid=$obj['lid'];
  20. $uid=$obj['uid'];
  21. $quantity=$obj['quantity'];
  22. $renew=$obj['renew'];
  23. $notes=$obj['notes'];
  24. //Checking if User already exist or not in MySQL database using SQL query.
  25. $query ="INSERT INTO `Prod_Buy`(`name_p`, `uid`, `lid`, `bought_date`, `expiration_date`, `type`, `quantity`, `renew`, `notes`) VALUES ('$name_p',$uid,$lid,'$bought_date','$expiration_date','$type',$quantity,$renew,'$notes')";
  26. // Executing SQL Query.
  27. if(empty($name_p) or empty($bought_date) or empty($expiration_date) or empty($type) or empty($lid) or empty($uid) or empty($uid) or empty($quantity) or empty($renew) or empty($notes)){
  28. $MSG = 'Field is empty. Enter information again...' ;
  29. // Converting the message into JSON format.
  30. $json = json_encode($MSG);
  31. // Echo, Print the message on screen.
  32. echo $json;
  33. }
  34. else{
  35. if(mysqli_query($con,$query)){
  36. // Show the success message.
  37. $MSG = 'Product Registered Successfully' ;
  38. // Converting the message into JSON format.
  39. $json = json_encode($MSG);
  40. echo $json;
  41. }
  42. else{
  43. $MSG = "Name for product already exists. Enter new one" ;
  44. // Converting the message into JSON format.
  45. $json = json_encode($MSG);
  46. // Echo, Print the message on screen.
  47. echo $json;
  48. }
  49. }
  50. mysqli_close($con);
  51. ?>