-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddItem.php
More file actions
50 lines (41 loc) · 1.54 KB
/
Copy pathaddItem.php
File metadata and controls
50 lines (41 loc) · 1.54 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
<?php
session_start();
if (!isset($_SESSION['userid'])) {
// If not logged in, redirect to the login page
header("Location: login.html");
exit;
}
$userid = $_SESSION['userid'];
include 'config.php';
// Establish database connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(json_encode(['success' => false, 'message' => 'Database connection failed: ' . $conn->connect_error]));
}
// Check if POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve and sanitize input
$item = isset($_POST['item']) ? trim($_POST['item']) : 'NULL';
$link = isset($_POST['link']) ? trim($_POST['link']) : 'NULL';
$multiple = isset($_POST['recurring']) ? 1 : 0;
$itemid = isset($_POST['itemid']) ? $_POST['itemid'] : 0;
if ($itemid == 0) {
// create new item
$stmt = $conn->prepare('insert into list (userid, text, link, recurring) values (?, ?, ?, ?)');
if ($stmt) {
$stmt->bind_param('issi', $userid, $item, $link, $multiple);
$stmt->execute();
header('Location: main.php');
} else die('insert failed');
} else {
// edit item
$stmt = $conn->prepare('update list set text = ?, link = ?, recurring = ? where id = ? and userid = ?');
if ($stmt) {
$stmt->bind_param('ssiii', $item, $link, $multiple, $itemid, $userid);
$stmt->execute();
header('Location: main.php');
} else die('insert failed');
}
} else die("no post");
?>