0% found this document useful (0 votes)
10 views

Code

This document defines a student record system with PHP functions to create, view, update, and delete student records from a MySQL database. It includes HTML and CSS to display forms for adding, viewing, and updating students. When forms are submitted, the PHP functions make corresponding database queries.

Uploaded by

kennromano05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Code

This document defines a student record system with PHP functions to create, view, update, and delete student records from a MySQL database. It includes HTML and CSS to display forms for adding, viewing, and updating students. When forms are submitted, the PHP functions make corresponding database queries.

Uploaded by

kennromano05
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

<!

DOCTYPE html>

<html lang=”en”>

<head>

<meta charset=”UTF-8”>

<meta name=”viewport” content=”width=device-width, initial-scale=1.0”>

<title>Student Record System</title>

<link rel=”stylesheet” href=”style.css”>

</head>

<body>

<header>

<h1>Student Record System</h1>

</header>

<?php

// Database connection

$servername = “localhost:3306”;

$username = “root”;

$password = “root”;

$dbname = “school”;

$conn = new mysqli($servername, $username, $password, $dbname);

If ($conn->connect_error) {

Die(“Connection failed: “ . $conn->connect_error);

// Functions

Function createStudent($conn, $name, $age, $grade) {


$sql = “INSERT INTO students (name, age, grade) VALUES (‘$name’, $age, $grade)”;

Return $conn->query($sql);

Function viewStudents($conn) {

$sql = “SELECT * FROM students”;

$result = $conn->query($sql);

Return $result->fetch_all(MYSQLI_ASSOC);

Function updateStudent($conn, $id, $name, $age, $grade) {

$sql = “UPDATE students SET name=’$name’, age=$age, grade=$grade WHERE id=$id”;

Return $conn->query($sql);

Function deleteStudent($conn, $id) {

$sql = “DELETE FROM students WHERE id=$id”;

Return $conn->query($sql);

// Handle form submissions

If ($_SERVER[“REQUEST_METHOD”] == “POST”) {

If (isset($_POST[“create”])) {

createStudent($conn, $_POST[“name”], $_POST[“age”], $_POST[“grade”]);

} elseif (isset($_POST[“update”])) {

updateStudent($conn, $_POST[“id”], $_POST[“name”], $_POST[“age”], $_POST[“grade”]);

} elseif (isset($_POST[“delete”])) {

deleteStudent($conn, $_POST[“id”]);

}
}

?>

<main>

<!—Create Form 

<form method=”post”>

<h2>Add Student</h2>

<label for=”name”>Name:</label>

<input type=”text” name=”name” required>

<label for=”age”>Age:</label>

<input type=”number” name=”age” required>

<label for=”grade”>Grade:</label>

<input type=”number” name=”grade” required>

<input type=”submit” name=”create” value=”Add”>

</form>

<!—View Students 

<table>

<tr>

<th>ID</th>

<th>Name</th>

<th>Age</th>

<th>Grade</th>

<th>Action</th>

</tr>

<?php

// Fetch students from the database

$students = viewStudents($conn);
// Check if there are any students

If ($students) {

Foreach ($students as $student) {

Echo “<tr>”;

Echo “<td>{$student[‘id’]}</td>”;

Echo “<td>{$student[‘name’]}</td>”;

Echo “<td>{$student[‘age’]}</td>”;

Echo “<td>{$student[‘grade’]}</td>”;

Echo “<td class=’action-buttons’>

<form method=’post’>

<input type=’hidden’ name=’id’ value=’{$student[‘id’]}’>

<input type=’submit’ name=’update’ value=’Update’ class=’update-btn’>

</form>

<form method=’post’ onsubmit=’return confirm(\”Are you sure you want to delete this
student?\”)’>

<input type=’hidden’ name=’id’ value=’{$student[‘id’]}’>

<input type=’submit’ name=’delete’ value=’Delete’ class=’delete-btn’>

</form>

</td>”;

Echo “</tr>”;

} else {

Echo “<tr><td colspan=’5’>No students found.</td></tr>”;

?>

</table>

<!—Update Form 
<?php

If (isset($_POST[“update”])) {

$studentToUpdate = viewStudents($conn, $_POST[“id”]);

Echo “<form method=’post’ class=’update-form’ onsubmit=’return confirm(\”Are you sure you want to
update this student?\”)’>”;

Echo “<h2>Update Student</h2>”;

Echo “<label for=’id’>ID:</label>”;

Echo “<input type=’text’ name=’id’ value=’{$studentToUpdate[‘id’]}’>”;

Echo “<label for=’name’>Name:</label>”;

Echo “<input type=’text’ name=’name’ value=’{$studentToUpdate[‘name’]}’ required>”;

Echo “<label for=’age’>Age:</label>”;

Echo “<input type=’number’ name=’age’ value=’{$studentToUpdate[‘age’]}’ required>”;

Echo “<label for=’grade’>Grade:</label>”;

Echo “<input type=’number’ name=’grade’ value=’{$studentToUpdate[‘grade’]}’ required>”;

Echo “<input type=’submit’ name=’update’ value=’Update’>”;

Echo “</form>”;

?>

</main>

</body>

</html>

<?php

// Close the database connection

$conn->close();

?>
Body {

Font-family: ‘Arial’, sans-serif;

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;

Text-shadow: 2px 2px 4px #333;

Main {

Max-width: 800px;

Margin: 20px auto;

Background-color: #fff;

Padding: 20px;

Border-radius: 8px;

Box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

}
Form {

Margin-bottom: 20px;

Label {

Display: block;

Margin-bottom: 8px;

Color: #333;

Input {

Width: 100%;

Padding: 8px;

Margin-bottom: 15px;

Border: 1px solid #ddd;

Border-radius: 4px;

Box-sizing: border-box;

Input[type=”submit”] {

Background-color: #3498db;

Color: #fff;

Border: none;

Padding: 10px 15px;

Border-radius: 4px;

Cursor: pointer;

Transition: background-color 0.3s;

}
Input[type=”submit”]:hover {

Background-color: #2980b9;

Table {

Width: 100%;

Border-collapse: collapse;

Margin-top: 20px;

Th, td {

Border: 1px solid #ddd;

Padding: 10px;

Text-align: left;

Th {

Background-color: #3498db;

Color: #fff;

.action-buttons {

Display: flex;

Gap: 5px;

Justify-content: center; /* Add this line */

.update-btn, .delete-btn {
Background-color: #2ecc71;

Color: #fff;

Border: none;

Padding: 5px 10px;

Border-radius: 4px;

Cursor: pointer;

Transition: background-color 0.3s;

.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: 1px solid #ddd;

Border-radius: 8px;

Box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

You might also like