Procházet zdrojové kódy

Upload files to ''

Add insert_productsss.php which is the api for inserting products
laura.gonzalez19 před 4 roky
rodič
revize
246cbc28c4
1 změnil soubory, kde provedl 82 přidání a 0 odebrání
  1. 82
    0
      insert_productsss.php

+ 82
- 0
insert_productsss.php Zobrazit soubor

@@ -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
+ $uid=$obj['uid'];
33
+ $quantity=$obj['quantity'];
34
+ $renew=$obj['renew'];
35
+ $notes=$obj['notes'];
36
+ 
37
+ 
38
+ //Checking if User already exist or not in MySQL database using SQL query.
39
+ $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')";
40
+ 
41
+ // Executing SQL Query.
42
+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)){ 
43
+
44
+
45
+$MSG = 'Field is empty. Enter information again...' ;
46
+ 
47
+ // Converting the message into JSON format.
48
+ $json = json_encode($MSG);
49
+ 
50
+ // Echo, Print the message on screen.
51
+
52
+echo $json;
53
+ 
54
+ }
55
+ 
56
+ else{
57
+ 
58
+ if(mysqli_query($con,$query)){
59
+ 
60
+ // Show the success message.
61
+ $MSG = 'Product Registered Successfully' ;
62
+ 
63
+ // Converting the message into JSON format.
64
+ $json = json_encode($MSG);
65
+
66
+echo $json; 
67
+ }
68
+ else{
69
+
70
+ $MSG = "Name for product already exists. Enter new one" ;
71
+ // Converting the message into JSON format.
72
+ $json = json_encode($MSG);
73
+ 
74
+ // Echo, Print the message on screen.
75
+ echo $json;
76
+
77
+ }
78
+ }
79
+ 
80
+ mysqli_close($con);
81
+?>
82
+