<?php

#Connecting to the GroceryPolice Database

$server = "localhost";
$user = "GroceryPolice";
$passwd = "P5B64tTUmGljk7cB";
$db = "GroceryPolice";

//Opens a new connection to the MySQL server

$con= mysqli_connect($server,$user, $passwd, $db);
 

//Receives JSON file from the app

// Getting the received JSON into $Received_JSON variable.
 $Received_JSON = file_get_contents('php://input');
 
// decoding the received JSON and store into $obj variable.
 $obj = json_decode($Received_JSON,true);
 
 // Populate from JSON $obj array and store into $user_name variable.
 


 $name_p=$obj['name_p'];
 $bought_date=$obj['bought_date'];
 $expiration_date=$obj['expiration_date'];
 $type=$obj['type'];
 $lid=$obj['lid'];
 $quantity=$obj['quantity'];
 $renew=$obj['renew'];
 $notes=$obj['notes'];
 
 
 //Checking if User already exist or not in MySQL database using SQL query.
 $query ="INSERT INTO `Product`(`name_p`, `bought_date`, `expiration_date`, `type`, `quantity`, `renew`, `notes`, `lid`) VALUES ('$name_p','$bought_date','$expiration_date','$type',$quantity,$renew,'$notes',$lid)";
 
 // Executing SQL Query.
if(empty($name_p) or empty($bought_date) or empty($expiration_date) or empty($type) or empty($lid) or empty($quantity) or empty($renew) or empty($notes)){ 


	$MSG = 'Field is empty. Enter information again...' ;
	 
	 // Converting the message into JSON format.
	 $json = json_encode($MSG);
	 
	 // Echo, Print the message on screen.

	echo $json;
 
 }
 
 else{
 
	 if(mysqli_query($con,$query)){
	 
	 // Show the success message.
	 $MSG = 'Product Registered Successfully' ;
	 
	 // Converting the message into JSON format.
	 $json = json_encode($MSG);

	echo $json; 
	}
	 else{

	 $MSG ="Error. Try again" ;
	 // Converting the message into JSON format.
	 $json = json_encode($MSG);
	 
	 // Echo, Print the message on screen.
	 echo $json;

	 }
 }
 
 mysqli_close($con);
?>