-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertPro.php
152 lines (120 loc) · 5.49 KB
/
insertPro.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
function cors() {
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
echo "You have CORS!";
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') :
http_response_code(405);
echo json_encode([
'success' => 0,
'message' => 'Invalid Request Method. HTTP method should be POST',
]);
exit;
endif;
require 'databasePostgre.php';
// $database = new Database();
// $conn = $database->dbConnection();
global $conn;
$data = json_decode(file_get_contents("php://input"));
if (!isset($data->title) || !isset($data->description)
|| !isset($data->images) || !isset($data->price)
|| !isset($data->link) || !isset($data->category) || !isset($data->subCategory)
|| !isset($data->featured) || !isset($data->ratings) || !isset($data->occasions) || !isset($data->brand)) :
echo json_encode([
'success' => 0,
'message' => 'Please fill all the fields | title , description , country, images, price , link , category , age section , featured , ratings , occasions , brand.',
]);
exit;
// titile , description , country, images, price , link , category , subCategory , featured , ratings , occasions , brand.
elseif (empty(trim($data->title)) || empty(trim($data->description)) || empty(trim($data->images))
|| empty(trim($data->price))|| empty(trim($data->link))|| empty(trim($data->category))|| empty(trim($data->subCategory))
|| empty(trim($data->featured)) || empty(trim($data->ratings)) || empty(trim($data->occasions))|| empty(trim($data->brand))) :
echo json_encode([
'success' => 0,
'message' => 'Oops! empty field detected. Please fill all the fields.',
]);
exit;
endif;
try {
$title = htmlspecialchars(trim($data->title));
$description = htmlspecialchars(trim($data->description));
// $country = htmlspecialchars(trim($data->country));
$images = htmlspecialchars(trim($data->images));
$price = htmlspecialchars(trim($data->price));
$link = htmlspecialchars(trim($data->link));
$category = htmlspecialchars(trim($data->category));
$subCategory = htmlspecialchars(trim($data->subCategory));
$featured = htmlspecialchars(trim($data->featured));
$ratings = htmlspecialchars(trim($data->ratings));
$occasions = htmlspecialchars(trim($data->occasions));
$brand = htmlspecialchars(trim($data->brand));
$query = "INSERT INTO product(title,description,images,price,link,category,subCategory,featured,ratings,occasions,brand)
VALUES('$title','$description','$images','$price','$link','$category','$subCategory','$featured','$ratings','$occasions','$brand')";
// $stmt = $conn->prepare($query);
$stmt = pg_query($conn, $query);
// $stmt->bindValue(':title', $title, PDO::PARAM_STR);
// $stmt->bindValue(':description', $description, PDO::PARAM_STR);
// // $stmt->bindValue(':country', $country, PDO::PARAM_STR);
// $stmt->bindValue(':images', $images, PDO::PARAM_STR);
// $stmt->bindValue(':price', $price, PDO::PARAM_STR);
// $stmt->bindValue(':link', $link, PDO::PARAM_STR);
// $stmt->bindValue(':category', $category, PDO::PARAM_STR);
// $stmt->bindValue(':subCategory', $subCategory, PDO::PARAM_STR);
// $stmt->bindValue(':featured', $featured, PDO::PARAM_STR);
// $stmt->bindValue(':ratings', $ratings, PDO::PARAM_STR);
// $stmt->bindValue(':occasions', $occasions, PDO::PARAM_STR);
// $stmt->bindValue(':brand', $brand, PDO::PARAM_STR);
if ($stmt) {
http_response_code(201);
echo json_encode([
'success' => 1,
'message' => 'Data Inserted Successfully.'
]);
exit;
}
echo json_encode([
'success' => 0,
'message' => 'Data not Inserted.'
]);
exit;
} catch (PDOException $e) {
http_response_code(500);
echo json_encode([
'success' => 0,
'message' => $e->getMessage()
]);
exit;
}
// function insert($data)
// {
// global $conn;
// $mine_result = $data['title'];
// // $mine_color = $data['mine_color'];
// // $date = $data['install_date'];
// // $owner = $data['mine_owner'];
// $query = "INSERT INTO product(title) VALUES('title')";
// // $query = "INSERT INTO mining_tb(mine_result,mine_color,install_date,mine_owner) VALUES('$mine_result','$mine_color','$date','$owner')";
// $insert = pg_query($conn, $query);
// return pg_affected_rows($insert);
// }