-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.php
149 lines (126 loc) · 5.13 KB
/
add.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Product</title>
</head>
<body>
<?php
include_once('dbconfig.in.php');
include_once('product.php');
my_header();
?>
<hr>
<?php
if (isset($_POST['insert'])) {
if (empty($_POST['name'])) {
echo "Please enter product name";
} elseif (empty($_POST['price'])) {
echo "Please enter product price";
} elseif (empty($_POST['quantity'])) {
echo "Please enter product quantity";
} elseif (empty($_POST['rating'])) {
echo "Please enter product Rating";
} elseif (empty($_POST['description'])) {
echo "Please enter product Description";
} elseif ($_POST['category'] === "Select Category") {
echo "Please select a category.";
} else {
try {
$pdo = conection_database();
$sql = $pdo->prepare("SELECT * FROM products ORDER BY id DESC LIMIT 1");
$sql->execute();
$lastElement = $sql->fetchObject('Product');
if (!$lastElement) {
$lastId = 1;
} else {
$lastId = $lastElement->getId() + 1;
}
$query = "INSERT INTO products (name,category,price,quantity,rating,description,imageName)
values(:name , :category , :price , :quantity , :rating , :description , :imageName)";
$result = $pdo->prepare($query);
$result->bindValue(':name', $_POST['name']);
$result->bindValue(':category', $_POST['category']);
$result->bindValue(':price', $_POST['price']);
$result->bindValue(':quantity', $_POST['quantity']);
$result->bindValue(':rating', $_POST['rating']);
$result->bindValue(':description', $_POST['description']);
if (isset($_FILES['imageName']) && !empty($_FILES['imageName']['name'])) {
$targetDir = "images/";
$tempFile = $_FILES['imageName']['tmp_name'];
$extension = pathinfo(basename($_FILES['imageName']['name']), PATHINFO_EXTENSION);
$newImageName = $lastId . "." . $extension;
$targetFile = $targetDir . $newImageName;
if (move_uploaded_file($tempFile, $targetFile)) {
$result->bindValue(':imageName', $targetFile);
$finalResult = $result->execute();
header('Location:products.php');
exit;
} else {
echo "Sorry, there was an error uploading your file.";
}
} else {
echo "Please Upload photo";
}
} catch (PDOException $e) {
die($e->getMessage());
}
}
} else {
?>
<main>
<article>
<form action="add.php" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Product Record</legend>
<p><label>Product Name</label> <input type="text" name="name"></p>
<p>
<label>Category</label>
<select name="category" id="category">
<option name="selectCategory">Select Category</option>
<option name="Cap">Cap</option>
<option name="T-Shirt">T-Shirt</option>
<option name="Shoes">Shoes</option>
</select>
</p>
<p>
<label>Price <input type="text" name="price"></label>
</p>
<p>
<label>Quantity <input type="number" name="quantity"></label>
</p>
<p>
<label>Rating <input type="number" name="rating"></label>
</p>
<div>
<label>Description: </label>
<br>
<textarea name="description" id="description" cols="80" rows="6"></textarea>
</div>
<p>
<label>
Product Photo:
</label>
<input type="file" name="imageName">
</p>
<p>
<input type="submit" name="insert" value='Insert'>
</p>
</fieldset>
</form>
</article>
</main>
<hr>
<?php
}
$pdo = null;
?>
<br>
<br>
<br>
<?php
my_footer();
?>
</body>
</html>