-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert.php
72 lines (60 loc) · 2 KB
/
Insert.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
<?php
$errors = [];
$data = [];
$CarID = $_POST['CarID'];
$Model = $_POST['Model'];
$Year = $_POST['Year'];
$Color = $_POST['Color'];
$Status = $_POST['Status'];
$Office = $_POST['Office'];
$PricePerDay = $_POST['PricePerDay'];
if (empty($CarID) or empty($Model)
or empty($Year) or empty($Color)
or empty($Status) or empty($Office)
or empty($PricePerDay)) {
$data['success'] = false;
alert("Something is missing");
echo json_encode($data);
return;
}
$connection = mysqli_connect('localhost', 'root', '', 'car_rental_system');
if (!$connection) {
return;
}
$query = mysqli_query($connection, "SELECT * FROM car WHERE car_id ='" . $CarID . "'");
$num_rows = mysqli_num_rows($query);
// console.log($num_rows);
$data['success'] = $num_rows == 0;
if ($num_rows == 0) {
$sql = "INSERT INTO car (car_id,
model,
year,
color,
`status`,
off_id,
is_reserved,
price_per_day)
VALUES(
'" . $CarID . "',
'" . $Model . "',
'" . $Year . "',
'" . $Color . "',
'" . $Status . "',
'" . $Office . "',
'false',
'" . $PricePerDay. "'
)";
$result = mysqli_query($connection, $sql);
if ($result) {
$data['success'] = true;
$data['message'] = 'Car Inserted Successfully !';
} else {
$data['success'] = false;
$data['message'] = 'ERROR Inserting to table !';
}
}else{
$data['success'] = false;
$data['message'] = 'This Car is already in the system';
}
mysqli_close($connection);
echo json_encode($data);