-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_friend.php
More file actions
executable file
·61 lines (50 loc) · 1.54 KB
/
delete_friend.php
File metadata and controls
executable file
·61 lines (50 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
51
52
53
54
55
56
57
58
59
60
61
<?php
session_start();
if (isset($_COOKIE['admin'])) {
$_SESSION['admin'] = $_COOKIE['admin'];
}
if (!isset($_SESSION['admin'])) {
header("Location: login.php");
exit();
}
?>
<?php
/*
// Include database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "shehda_library_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
*/
// include "db_connect.php";
require_once "db_connect.php"; // require once is better than include, because the database connection is important.
// Check if friend ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
die("Invalid friend ID.");
}
$friend_id = $_GET['id'];
// Check if the friend has borrowed books
$check_borrow_sql = "SELECT * FROM borrows WHERE friend_id = ?";
$stmt = $conn->prepare($check_borrow_sql);
$stmt->bind_param("i", $friend_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "<script>alert('Cannot delete this friend. They have borrowed books.'); window.location='index.php';</script>";
exit();
}
// Delete friend from the database
$sql = "DELETE FROM friends WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $friend_id);
if ($stmt->execute()) {
echo "<script>alert('Friend deleted successfully!'); window.location='index.php';</script>";
} else {
echo "Error deleting friend: " . $conn->error;
}
$conn->close();
?>