Student CRUD

  1. Create Student

<!DOCTYPE html>
<html>
<head>
	<title>Add Student</title>
	<style>
		label, input {
			display: block;
			margin-bottom: 10px;
		}
	</style>
</head>
<body>
	<h1>Add Student</h1>
	<form method="post" action="add_student.php">
		<label>Name:</label>
		<input type="text" name="name" required>

		<label>Email:</label>
		<input type="email" name="email" required>

		<label>Phone:</label>
		<input type="tel" name="phone" required>

		<label>Address:</label>
		<textarea name="address"></textarea>

		<input type="submit" value="Add Student">
	</form>
</body>
</html>

2.

<?php
// Include the database connection file
require_once 'db_connect.php';

// Get the form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];

// Prepare the SQL statement
$sql = "INSERT INTO students (name, email, phone, address) VALUES (?, ?, ?, ?)";

// Bind the parameters and execute the statement
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss", $name, $email, $phone, $address);
$stmt->execute();

// Redirect back to the form
header("Location: add_student_form.php");
exit;
?>
  1. Display Students

<!DOCTYPE html>
<html>
<head>
	<title>View Students</title>
	<style>
		table {
			border-collapse: collapse;
			width: 100%;
		}
		th, td {
			padding: 8px;
			text-align: left;
			border-bottom: 1px solid #ddd;
		}
	</style>
</head>
<body>
	<h1>View Students</h1>
	<table>
		<thead>
			<tr>
				<th>Name</th>
				<th>Email</th>
				<th>Phone</th>
				<th>Action</th>
			</tr>
		</thead>
		<tbody>
			<?php
			// Include the database connection file
			require_once 'db_connect.php';

			// Prepare the SQL statement
			$sql = "SELECT * FROM students ORDER BY id DESC";
			$result = $conn->query($sql);

			// Loop through the result and output the data
			while ($row = $result->fetch_assoc()) {
				echo "<tr>";
				echo "<td>" . $row['name'] . "</td>";
				echo "<td>" . $row['email'] . "</td>";
				echo "<td>" . $row['phone'] . "</td>";
				echo "<td>";
				echo "<a href='edit_student_form.php?id=" . $row['id'] . "'>Edit</a> ";
				echo "<a href='delete_student.php?id=" . $row['id'] . "' onclick='return confirm(\"Are you sure?\")'>Delete</a>";
				echo "</td>";
				echo "</tr>";
			}

			// Close the database connection
			$conn->close();
			?>
		</tbody>
	</table>
</body>
</html>
  1. Edit Student

<!-- edit_student_form.php -->
<!DOCTYPE html>
<html>
<head>
	<title>Edit Student Form</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<h1>Edit Student</h1>
	<?php
	  // Retrieve the ID from the URL parameter
	  $id = $_GET['id'];
	  // Connect to the database
	  require_once 'db_connect.php';
	  // Check connection
	  if (!$conn) {
	    die("Connection failed: " . mysqli_connect_error());
	  }
	  // Query to retrieve the row with the specified ID from the 'users' table
	  $sql = "SELECT * FROM students WHERE id=$id";
	  $result = mysqli_query($conn, $sql);
	  // Fetch the row as an associative array
	  $row = mysqli_fetch_assoc($result);
	  // Close the database connection
	  mysqli_close($conn);
	  ?>

	<form method="POST" action="edit_student.php">
		<input type="hidden" name="id" value="<?php echo $id; ?>">
		<label for="name">Name:</label>
		<input type="text" name="name" value="<?php echo $row["name"]; ?>">

		<label for="email">Email:</label>
		<input type="email" name="email" value="<?php echo $row["email"]; ?>">

		<label for="phone">Phone:</label>
		<input type="tel" name="phone" value="<?php echo $row["phone"]; ?>">

		<label for="address">Address:</label>
		<textarea name="address"><?php echo $row["address"]; ?></textarea>

		<input type="submit" name="submit" value="Update Student">
	</form>

	<a href="view_students.php">Back to List</a>
</body>
</html>
<?php 
	// Include the database connection file
	require_once 'db_connect.php';

	// Get the form data
	$id = $_POST['id'];
	$name = $_POST['name'];
	$email = $_POST['email'];
	$phone = $_POST['phone'];
	$address = $_POST['address'];
	// Update the student record in the database
	$sql = "UPDATE students SET name='$name', email='$email', phone='$phone', address='$address' WHERE id=$id";
	if (mysqli_query($conn, $sql)) {
	    echo "Student record updated successfully!";
	} else {
	    echo "Error updating student record: " . mysqli_error($conn);
	}

Last updated