-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-addCategories.php
More file actions
68 lines (62 loc) · 2.01 KB
/
Copy pathadmin-addCategories.php
File metadata and controls
68 lines (62 loc) · 2.01 KB
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
<?php
require_once "user.php";
CheckIfAdminLoggedIn();
include 'Geeksforsaletop.php';
if(isset($_POST['categoryName'])){
// Add a new top category
$sql = 'INSERT into productcategory (categoryName) values (:categoryName)';
$sth = $db->prepare($sql);
$sth->bindValue (':categoryName', $_POST['categoryName']);
if($sth->execute()){
header( 'Location: admin-addCategories.php' );
}
}
else if(isset($_POST['subcategoryName']))
{
// Add new sub category
$sql = 'select id from productcategory where categoryName = :categoryName';
$sth = $db->prepare($sql);
$sth->bindValue (':categoryName', $_POST['category']);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
// only add if we only found one category
if($sth->rowCount() === 1)
{
// Add the new sub category
$row = $sth->fetch();
$id = $row['id'];
$sql = 'INSERT into subcategory (name, categoryid) values (:name, :categoryid)';
$sth2 = $db->prepare($sql);
$sth2->bindValue (':name', $_POST['subcategoryName']);
$sth2->bindValue (':categoryid', $id);
if($sth2->execute())
{
header( 'Location: admin-addCategories.php' );
}
}
}
?>
<div id= "content">
<?php include 'admin-buttons.php'; ?>
<p> Add a new top category
<form action="admin-addCategories.php" method="post">
<input type="text" name="categoryName" required />
<input type="submit" name="submit" value="Add top category">
</form>
<p> Add a new sub category
<form action="admin-addCategories.php" method="post">
<select name="category">
<?php
$sql = 'select categoryName from productcategory';
$sth = $db->prepare($sql);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($row = $sth->fetch()){
echo "<option value=\"".$row['categoryName']."\">".$row['categoryName']."</option>";
}
?>
</select>
<input type="text" name="subcategoryName" required />
<input type="submit" name="submit" value="Add subcategory">
</form>
</div>