Weblab 4
Weblab 4
M
Ex.No.4
STUDENT MANAGEMENT SYSTEM USING PHP
Aim
To write a HTML, PHP code to implement the CRUD operation for the Student Management System.
Algorithm
STEP 1: Start the program.
STEP 2: Create the html page to read the student details by using the form.
STEP 3: Create php file to connect the webpage with the database and insert the data given by the user.
STEP 4: Create a php file to read the data from the database and display the data.
STEP 5: Create a php file to delete the data from the database.
Program
Index.php
<?php
$conn = new mysqli('localhost', 'root', '', 'student_db');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['save'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$roll_no = isset($_POST['roll_no']) ? $_POST['roll_no'] : '';
$department = isset($_POST['department']) ? $_POST['department'] : '';
$id = $_POST['id'];
if ($id == '') {
$stmt = $conn->prepare("INSERT INTO students (name, age, email, roll_no, department)
VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sisss", $name, $age, $email, $roll_no, $department);
$stmt->execute();
$stmt->close();
} else {
$stmt = $conn->prepare("UPDATE students SET name=?, age=?, email=?, roll_no=?,
department=? WHERE id=?");
$stmt->bind_param("sisssi", $name, $age, $email, $roll_no, $department, $id);
$stmt->execute();
$stmt->close();
}
}
if (isset($_GET['delete'])) {
22UIT503/ WEB PROGRAMMING LABORATORY
710722205302 SURYA PRAKATHEESH
M
$id = $_GET['delete'];
$stmt = $conn->prepare("DELETE FROM students WHERE id=?");
$stmt->bind_param("i", $id);a
$stmt->execute();
$stmt->close();
}
$edit = null;
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$stmt = $conn->prepare("SELECT * FROM students WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$edit = $result->fetch_assoc();
$stmt->close();
}
$result = $conn->query("SELECT * FROM students");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Management System</title>
</head>
<body style="font-family: Arial, sans-serif; text-align: center; background-color: #f9f9f9;">
<h2 style="color: #333;">Student Management System</h2>
<form method="POST" action="" style="margin-bottom: 20px; border: 1px solid #ccc; padding:
20px; border-radius: 5px; background-color: #fff;">
<input type="hidden" name="id" value="<?= $edit ? $edit['id'] : '' ?>">
<label>Name:</label>
<input type="text" name="name" value="<?= $edit ? $edit['name'] : '' ?>" required
style="margin-bottom: 10px;"><br>
<label>Age:</label>
<input type="number" name="age" value="<?= $edit ? $edit['age'] : '' ?>" required
style="margin-bottom: 10px;"><br>
<label>Email:</label>
<input type="email" name="email" value="<?= $edit ? $edit['email'] : '' ?>" required
style="margin-bottom: 10px;"><br>
<label>Roll No:</label>
<input type="text" name="roll_no" value="<?= $edit ? $edit['roll_no'] : '' ?>" required
style="margin-bottom: 10px;"><br>
<label>Department:</label>
<input type="text" name="department" value="<?= $edit ? $edit['department'] : '' ?>" required
style="margin-bottom: 10px;"><br>
<button type="submit" name="save" style="background-color: #4CAF50; color: white; padding:
10px 15px; border: none; border-radius: 5px; cursor: pointer;"><?= $edit ? 'Update' : 'Save'
?></button>
</form>
<h3 style="color: #333;">Student List</h3>
<table border="1" style="border-collapse: collapse; width: 80%; margin: auto;">
<tr style="background-color: #f2f2f2;">
<th>ID</th>
<th>Name</th>
<th>Age</th>
22UIT503/ WEB PROGRAMMING LABORATORY
710722205302 SURYA PRAKATHEESH
M
<th>Email</th>
<th>Roll No</th>
<th>Department</th>
<th>Actions</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td style="border: 1px solid #ccc; padding: 8px;"><?= $row['id'] ?></td>
<td style="border: 1px solid #ccc; padding: 8px;"><?= $row['name'] ?></td>
<td style="border: 1px solid #ccc; padding: 8px;"><?= $row['age'] ?></td>
<td style="border: 1px solid #ccc; padding: 8px;"><?= $row['email'] ?></td>
<td style="border: 1px solid #ccc; padding: 8px;"><?= $row['roll_no'] ?></td>
<td style="border: 1px solid #ccc; padding: 8px;"><?= $row['department'] ?></td>
<td style="border: 1px solid #ccc; padding: 8px;">
<a href="?edit=<?= $row['id'] ?>" style="text-decoration: none; color: blue;">Edit</a> |
<a href="?delete=<?= $row['id'] ?>" style="text-decoration: none; color: red;">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>
Output:
CREATE :
UPDATE:
DELETE :
Result
Thus, the aim to create a webpage for the student management system to perform CRUD using PHP is
successfully achieved and verified.