forked from ggreenaz/dadCHECKIN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depart.php
139 lines (121 loc) · 4.33 KB
/
depart.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
// Error reporting and PHP settings
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include the database configuration file
require_once './config.php';
// Get the database connection
$conn = getDBConnection();
// Define the function to fetch visitor data
function fetchVisitorData($conn, $phone) {
$query = "SELECT first_name, last_name, phone, email, checkin_time, checkout_time FROM visitors WHERE phone = ? AND checkout_time IS NULL";
$stmt = $conn->prepare($query);
if ($stmt) {
$stmt->bind_param("s", $phone);
if ($stmt->execute()) {
$result = $stmt->get_result();
if ($result->num_rows > 0) {
return $result->fetch_assoc();
}
}
$stmt->close();
}
return null;
}
$checkedOutVisitor = null;
$successMessage = '';
$errorMessage = '';
// Handle Check-Out
if (isset($_POST['checkout'])) {
$checkout_phone = $conn->real_escape_string($_POST['checkout_phone']);
$checkout_time = date('Y-m-d H:i:s');
// Fetch visitor data before updating
$checkedOutVisitor = fetchVisitorData($conn, $checkout_phone);
if ($checkedOutVisitor) {
// Update the checkout_time in the database
$updateStmt = $conn->prepare("UPDATE visitors SET checkout_time = ? WHERE phone = ? AND checkout_time IS NULL");
$updateStmt->bind_param("ss", $checkout_time, $checkout_phone);
// Execute the update query
if ($updateStmt->execute()) {
// Check if the update was successful
if ($updateStmt->affected_rows > 0) {
// Update the checkout_time in the checkedOutVisitor array
$checkedOutVisitor['checkout_time'] = $checkout_time;
$successMessage = "Visitor checked out successfully!";
} else {
$errorMessage = "Error updating visitor data or no update needed.";
}
} else {
echo "Error updating checkout_time: " . $updateStmt->error;
}
$updateStmt->close();
} else {
$errorMessage = "No visitor found with that phone number.";
}
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Visitor Check-In and Check-Out</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="UTF-8">
<style>
/* CSS for centering elements in the table cell */
.center-cell {
text-align: center;
vertical-align: middle;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<img src="../img/dnd-project-sm-logo.png">
<h1>Visitor Management</h1>
<!-- Separate Form for Visitor Check-Out -->
<form method="POST">
<table style="width:40%; margin: 0 auto;" border="1">
<tr>
<td><label for="checkout_phone">Check-Out (Phone):</label></td>
<td class="center-cell">
<input type="text" name="checkout_phone" id="checkout_phone" placeholder="Phone Number" required>
<button type="submit" name="checkout">Log Out</button>
</td>
</tr>
</table>
</form>
<!-- Success message for Log Out -->
<?php if (!empty($successMessage)): ?>
<p><?= $successMessage ?></p>
<a href="./" class="button">Click Here to Finish Checking Out!</a>
<?php endif; ?>
<!-- Display "No visitor found with that phone number." message -->
<?php if (!empty($errorMessage)): ?>
<p><?= $errorMessage ?></p>
<?php endif; ?>
<!-- Display Visitor Information -->
<?php if ($checkedOutVisitor): ?>
<h2>Checked-Out Visitor Information</h2>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Check-Out Time</th>
</tr>
<tr>
<td><?= htmlspecialchars($checkedOutVisitor['first_name']) ?></td>
<td><?= htmlspecialchars($checkedOutVisitor['last_name']) ?></td>
<td><?= htmlspecialchars($checkedOutVisitor['phone']) ?></td>
<td><?= htmlspecialchars($checkedOutVisitor['email']) ?></td>
<td><?= htmlspecialchars($checkedOutVisitor['checkout_time']) ?></td>
</tr>
</table>
<?php endif; ?>
</body>
</html>