-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_AddCategory.php
35 lines (28 loc) · 1.11 KB
/
Process_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
<?php
include 'DBconnect.php';
if(isset($_POST['categoryName'])){
$categoryNames = $_POST['categoryName'];
// Split the category names by comma
$categories = explode(',', $categoryNames);
$ctr = 0;
foreach ($categories as $category) {
$category = trim($category); // Trim whitespace from each category name
// Check if category name already exists
$checkQuery = "SELECT * FROM category WHERE category_name='$category'";
$checkResult = mysqli_query($connect, $checkQuery);
if(mysqli_num_rows($checkResult) > 0){
echo "<div class='alert alert-danger'>Category '$category' already exists!</div>";
$ctr ++;
} else {
// Insert new category into database
$insertQuery = "INSERT INTO category (category_name) VALUES ('$category')";
if(!mysqli_query($connect, $insertQuery)){
echo "<div class='alert alert-danger'>Error adding category '$category'!</div>";
$ctr ++;
}
}
}
if($ctr == 0) echo "success";
mysqli_close($connect);
}
?>