Repository navigation
Expand file tree
/
Copy pathadmin_create_reward.php
More file actions
51 lines (46 loc) · 1.96 KB
/
Copy pathadmin_create_reward.php
File metadata and controls
51 lines (46 loc) · 1.96 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
<?php
include 'api/conn.php';
if (isset($_POST['submit_reward'])) {
$rewardID = $_POST['reward_id'] ?? null;
$title = $_POST['title'];
$category = $_POST['category'];
$description = $_POST['description'];
$price = $_POST['points'];
$stock = $_POST['stock'];
$mediaPath = "";
if (!empty($_FILES["reward_image"]["name"])) {
$targetDir = "uploads/rewards/";
$fileName = time() . "_" . basename($_FILES["reward_image"]["name"]);
$targetFilePath = $targetDir . $fileName;
if (move_uploaded_file($_FILES["reward_image"]["tmp_name"], $targetFilePath)) {
$mediaPath = $targetFilePath;
}
}
if ($rewardID) {
if ($mediaPath != "") {
$sql = "UPDATE REWARD SET title=?, category=?, description=?, price=?, media=?, remaining=? WHERE ID=?";
$stmt = $dbConnection->prepare($sql);
$stmt->bind_param("sssisis", $title, $category, $description, $price, $mediaPath, $stock, $rewardID);
} else {
$sql = "UPDATE REWARD SET title=?, category=?, description=?, price=?, remaining=? WHERE ID=?";
$stmt = $dbConnection->prepare($sql);
$stmt->bind_param("sssiis", $title, $category, $description, $price, $stock, $rewardID);
}
$msg = "reward_updated";
} else {
include './api/utils/creation_util.php';
$newID = generate_next_id($dbConnection, 'submission', 'ID', 'RW_');
$sql = "INSERT INTO REWARD (ID, title, category, description, price, media, active, remaining, initial)
VALUES (?, ?, ?, ?, ?, ?, 1, ?, ?)";
$stmt = $dbConnection->prepare($sql);
$stmt->bind_param("ssssisii", $newID, $title, $category, $description, $price, $mediaPath, $stock, $stock);
$msg = "reward_created";
}
if ($stmt->execute()) {
header("Location: admin.php?success=$msg");
exit();
} else {
echo "Database Error: " . $stmt->error;
}
}
?>