Przeglądaj źródła

Upload insert _p.php to master branch

laura.gonzalez19 3 lat temu
rodzic
commit
f5b6f93676
1 zmienionych plików z 82 dodań i 0 usunięć
  1. 82
    0
      insert_p.php

+ 82
- 0
insert_p.php Wyświetl plik

@@ -0,0 +1,82 @@
1
+<?php
2
+
3
+#Connecting to the GroceryPolice Database
4
+
5
+$server = "localhost";
6
+$user = "GroceryPolice";
7
+$passwd = "P5B64tTUmGljk7cB";
8
+$db = "GroceryPolice";
9
+
10
+//Opens a new connection to the MySQL server
11
+
12
+$con= mysqli_connect($server,$user, $passwd, $db);
13
+ 
14
+
15
+//Receives JSON file from the app
16
+
17
+// Getting the received JSON into $Received_JSON variable.
18
+ $Received_JSON = file_get_contents('php://input');
19
+ 
20
+// decoding the received JSON and store into $obj variable.
21
+ $obj = json_decode($Received_JSON,true);
22
+ 
23
+ // Populate from JSON $obj array and store into $user_name variable.
24
+ 
25
+
26
+
27
+ $name_p=$obj['name_p'];
28
+ $bought_date=$obj['bought_date'];
29
+ $expiration_date=$obj['expiration_date'];
30
+ $type=$obj['type'];
31
+ $lid=$obj['lid'];
32
+ $quantity=$obj['quantity'];
33
+ $renew=$obj['renew'];
34
+ $notes=$obj['notes'];
35
+ 
36
+ 
37
+ //Checking if User already exist or not in MySQL database using SQL query.
38
+ $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)";
39
+ 
40
+ // Executing SQL Query.
41
+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)){ 
42
+
43
+
44
+	$MSG = 'Field is empty. Enter information again...' ;
45
+	 
46
+	 // Converting the message into JSON format.
47
+	 $json = json_encode($MSG);
48
+	 
49
+	 // Echo, Print the message on screen.
50
+
51
+	echo $json;
52
+ 
53
+ }
54
+ 
55
+ else{
56
+ 
57
+	 if(mysqli_query($con,$query)){
58
+	 
59
+	 // Show the success message.
60
+	 $MSG = 'Product Registered Successfully' ;
61
+	 
62
+	 // Converting the message into JSON format.
63
+	 $json = json_encode($MSG);
64
+
65
+	echo $json; 
66
+	}
67
+	 else{
68
+
69
+	 $MSG ="Error. Try again" ;
70
+	 // Converting the message into JSON format.
71
+	 $json = json_encode($MSG);
72
+	 
73
+	 // Echo, Print the message on screen.
74
+	 echo $json;
75
+
76
+	 }
77
+ }
78
+ 
79
+ mysqli_close($con);
80
+?>
81
+
82
+