-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-custom.php
More file actions
40 lines (36 loc) · 1.3 KB
/
delete-custom.php
File metadata and controls
40 lines (36 loc) · 1.3 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
<?php
session_start();
// Check if the user is not logged in and redirect to the login page if necessary
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$dsn = 'mysql:host=localhost;dbname=fitfuelhub_db';
$username = 'root';
$password = '';
try {
$db = new PDO($dsn, $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get the meal ID from the form data
$mealId = $_POST['id'];
// Prepare and execute the DELETE query to delete the custom meal
$query = "DELETE FROM custom_meal WHERE id = :id AND user_id = :user_id";
$stmt = $db->prepare($query);
$stmt->bindParam(':id', $mealId, PDO::PARAM_INT);
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
// Redirect back to the recipes page after deletion
header("Location: recipes.php");
exit;
} catch (PDOException $e) {
echo 'Database Error: ' . $e->getMessage();
die();
}
} else {
// If the request method is not POST, redirect to the recipes page
header("Location: recipes.php");
exit;
}
?>