-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetrieve.php
70 lines (62 loc) · 2.16 KB
/
Retrieve.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
<!DOCTYPE html>
<html>
<head>
<title>Students Data</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
form {
display: inline;
}
</style>
</head>
<body>
<h2>Students Data</h2>
<a href="index.php">add a new student</a>
<?php
$host = 'localhost';
$dbname = 'unvirsity'; // Make sure the database name is spelled correctly
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT first_name, last_name, email, date_of_birth, gender, major, enrollment_year FROM students";
$stmt = $pdo->query($sql);
if ($stmt->rowCount() > 0) {
echo "<table>";
echo "<tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Date of Birth</th><th>Gender</th><th>Major</th><th>Enrollment Year</th><th>Actions</th></tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>".$row['first_name']."</td><td>".$row['last_name']."</td><td>".$row['email']."</td><td>".$row['date_of_birth']."</td><td>".$row['gender']."</td><td>".$row['major']."</td><td>".$row['enrollment_year']."</td>";
echo "<td>";
echo "<form action='update.php' method='POST'>";
echo "<input type='hidden' name='email' value='".$row['email']."'>";
echo "<input type='submit' value='Update'>";
echo "</form>";
echo "<form action='delete.php' method='POST'>";
echo "<input type='hidden' name='email' value='".$row['email']."'>";
echo "<input type='submit' value='Delete' onclick='return confirm(\"Are you sure?\");'>";
echo "</form>";
echo "</td></tr>";
}
echo "</table>";
} else {
echo "No results found.";
}
$pdo = null;
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
</body>
</html>