Code
Code
DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8”>
</head>
<body>
<header>
</header>
<?php
// Database connection
$servername = “localhost:3306”;
$username = “root”;
$password = “root”;
$dbname = “school”;
If ($conn->connect_error) {
// Functions
Return $conn->query($sql);
Function viewStudents($conn) {
$result = $conn->query($sql);
Return $result->fetch_all(MYSQLI_ASSOC);
Return $conn->query($sql);
Return $conn->query($sql);
If ($_SERVER[“REQUEST_METHOD”] == “POST”) {
If (isset($_POST[“create”])) {
} elseif (isset($_POST[“update”])) {
} elseif (isset($_POST[“delete”])) {
deleteStudent($conn, $_POST[“id”]);
}
}
?>
<main>
<!—Create Form
<form method=”post”>
<h2>Add Student</h2>
<label for=”name”>Name:</label>
<label for=”age”>Age:</label>
<label for=”grade”>Grade:</label>
</form>
<!—View Students
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Grade</th>
<th>Action</th>
</tr>
<?php
$students = viewStudents($conn);
// Check if there are any students
If ($students) {
Echo “<tr>”;
Echo “<td>{$student[‘id’]}</td>”;
Echo “<td>{$student[‘name’]}</td>”;
Echo “<td>{$student[‘age’]}</td>”;
Echo “<td>{$student[‘grade’]}</td>”;
<form method=’post’>
</form>
<form method=’post’ onsubmit=’return confirm(\”Are you sure you want to delete this
student?\”)’>
</form>
</td>”;
Echo “</tr>”;
} else {
?>
</table>
<!—Update Form
<?php
If (isset($_POST[“update”])) {
Echo “<form method=’post’ class=’update-form’ onsubmit=’return confirm(\”Are you sure you want to
update this student?\”)’>”;
Echo “</form>”;
?>
</main>
</body>
</html>
<?php
$conn->close();
?>
Body {
Background-color: #f0f0f0;
Margin: 0;
Padding: 0;
Header {
Background-color: #3498db;
Color: #fff;
Text-align: center;
Padding: 1em 0;
H1 {
Margin: 0;
Font-size: 2em;
Main {
Max-width: 800px;
Background-color: #fff;
Padding: 20px;
Border-radius: 8px;
}
Form {
Margin-bottom: 20px;
Label {
Display: block;
Margin-bottom: 8px;
Color: #333;
Input {
Width: 100%;
Padding: 8px;
Margin-bottom: 15px;
Border-radius: 4px;
Box-sizing: border-box;
Input[type=”submit”] {
Background-color: #3498db;
Color: #fff;
Border: none;
Border-radius: 4px;
Cursor: pointer;
}
Input[type=”submit”]:hover {
Background-color: #2980b9;
Table {
Width: 100%;
Border-collapse: collapse;
Margin-top: 20px;
Th, td {
Padding: 10px;
Text-align: left;
Th {
Background-color: #3498db;
Color: #fff;
.action-buttons {
Display: flex;
Gap: 5px;
.update-btn, .delete-btn {
Background-color: #2ecc71;
Color: #fff;
Border: none;
Border-radius: 4px;
Cursor: pointer;
.update-btn:hover, .delete-btn:hover {
Background-color: #27ae60;
.update-form {
Max-width: 50%;
Display: flex-direction;
Flex-direction: column;
Align-items: center;
Margin: 5px auto; /* Center horizontally and add some top margin */
Background-color: #ecf0f1;
Padding: 15px;
Border-radius: 8px;