This repository was archived by the owner on Mar 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborrow.php
More file actions
42 lines (35 loc) · 1.31 KB
/
borrow.php
File metadata and controls
42 lines (35 loc) · 1.31 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
<?php
session_start();
include 'includes/db.php';
/** @var mysqli $conn */
// 1. Security Check: Only logged-in users can borrow
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
// 2. Capture the Book ID from the link (e.g., borrow.php?book_id=5)
if (isset($_GET['book_id'])) {
$book_id = (int)$_GET['book_id']; // Cast to integer for safety
$user_id = $_SESSION['user_id'];
// 3. Automated Dates (Matching your table columns)
$issue_date = date('Y-m-d');
$return_date = date('Y-m-d', strtotime('+14 days'));
$status = 'Borrowed';
// 4. Prepared Statement for your 'transactions' table
$query = "INSERT INTO transactions (user_id, book_id, issue_date, return_date, status) VALUES (?, ?, ?, ?, ?)";
$stmt = $conn->prepare($query);
// "ii" for two integers, "sss" for three strings (dates & status)
$stmt->bind_param("iisss", $user_id, $book_id, $issue_date, $return_date, $status);
if ($stmt->execute()) {
// Success! Go back to dashboard
header("Location: dashboard.php?msg=borrow_success");
} else {
// Error handling
echo "Error recording transaction: " . $stmt->error;
}
$stmt->close();
} else {
// If no book was selected, send them to the search page
header("Location: search.php");
exit();
}