-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin_addcategory.php
47 lines (45 loc) · 1.43 KB
/
admin_addcategory.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
<?php
session_start();
if((!isset($_SESSION['manager']) && !isset($_SESSION['expert']))){
header("Location:index.php");
}
$title = "Add new category";
require "./template/header.php";
require "./functions/database_functions.php";
$conn = db_connect();
if(isset($_POST['add'])){
$name = trim($_POST['name']);
$name = mysqli_real_escape_string($conn, $name);
// find category and return catid
// if category is not in db, create new
$findCat = "SELECT * FROM category WHERE category_name = '$name'";
$findResult = mysqli_query($conn, $findCat);
if(mysqli_num_rows($findResult)==0){
// insert into category table and return id
$insertCat = "INSERT INTO category(category_name) VALUES ('$name')";
$insertResult = mysqli_query($conn, $insertCat);
if(!$insertResult){
echo "Can't add new category " . mysqli_error($conn);
exit;
}
header("Location: admin_categories.php");
} else {
echo '<p style="color:red;">category already exists</p>';
}
}
?>
<form method="post" action="admin_addcategory.php" enctype="multipart/form-data">
<table class="table">
<tr>
<th>Name</th>
<td><input type="text" name="name"></td>
</tr>
</table>
<input type="submit" name="add" value="Add new Category" class="btn btn-primary">
<a href="admin_categories.php" class="btn btn-default">Cancel</a>
</form>
<br/>
<?php
if(isset($conn)) {mysqli_close($conn);}
require_once "./template/footer.php";
?>