CRUD code for subjects

here's an example of how to create a Subject CRUD system in PHP and MySQL:

  1. Create a database table for the subjects:

CREATE TABLE `subjects` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `subject_code` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. Create a form to add a new subject:

<form action="add_subject.php" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required>
  <br>
  <label for="code">code:</label>
  <textarea id="subject_code" name="subject_code"></textarea>
  <br>
  <button type="submit">Add Subject</button>
</form>
  1. Create a PHP script to insert a new subject into the database (add_subject.php):

<?php
require_once 'db_connect.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $subject_code = $_POST['subject_code'];

    $sql = "INSERT INTO subjects (name, subject_code) VALUES ('$name', '$subject_code')";

    if (mysqli_query($conn, $sql)) {
    	echo "New contact added successfully";
    } else {
    	echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
    // redirect to the show page
    header('Location: view_subject.php');
}


?>
  1. Create a page to view all subjects:

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

// Query the database for all subjects
$sql = "SELECT * FROM subjects";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
  <title>Subjects</title>
</head>
<body>
  <h1>Subjects</h1>
  <a href="add_subject_form.php">Add Subject</a>
  <br><br>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Code</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
      <?php while($row = $result->fetch_assoc()) { ?>
        <tr>
          <td><?php echo $row['id']; ?></td>
          <td><?php echo $row['name']; ?></td>
          <td><?php echo $row['subject_code']; ?></td>
          <td>
            <a href="edit_subject_form.php?id=<?php echo $row['id']; ?>">Edit</a>
            <a href="delete_subject.php?id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure you want to delete this subject?')">Delete</a>
          </td>
        </tr>
      <?php } ?>
    </tbody>
  </table>
</body>
</html>
  1. Create a form to edit a subject:

<?php
    include 'db_connect.php'; // include database connection file

    if(isset($_POST['edit'])) {
        $subject_id = $_POST['id']; 
        $subject_name = $_POST['subject_name'];
        $subject_code = $_POST['subject_code'];
        
        $query = "UPDATE subjects SET name='$subject_name', subject_code='$subject_code' WHERE id=$subject_id";
        $result = mysqli_query($conn, $query);

        if($result) {
            header("Location: view_subject.php"); // redirect to subjects page after successful update
            exit();
        }
        else {
            echo "Error updating subject: " . mysqli_error($conn);
        }
    }

    // retrieve subject details for editing
    if(isset($_GET['id'])) {
        $id = $_GET['id'];

        $query = "SELECT * FROM subjects WHERE id=$id";
        $result = mysqli_query($conn, $query);

        if(mysqli_num_rows($result) == 1) {
            $row = mysqli_fetch_assoc($result);
            $subject_name = $row['name'];
            $subject_code = $row['subject_code'];
        }
        else {
            echo "Subject not found.";
        }
    }
?>

<!-- HTML form for editing subject -->
<h2>Edit Subject</h2>
<form method="post" action="">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <label>Subject Name:</label>
    <input type="text" name="subject_name" value="<?php echo $subject_name; ?>">
    <label>Subject Code:</label>
    <input type="text" name="subject_code" value="<?php echo $subject_code; ?>">
    <input type="submit" name="edit" value="Update">
</form>
  1. Delete Subject:

<?php
    include 'db_connect.php'; // include database connection file

    if(isset($_GET['delete_id'])) {
        $subject_id = $_GET['delete_id'];
        
        $query = "DELETE FROM subjects WHERE id=$subject_id";
        $result = mysqli_query($conn, $query);

        if($result) {
            header("Location: subjects.php"); // redirect to subjects page after successful delete
            exit();
        }
        else {
            echo "Error deleting subject: " . mysqli_error($conn);
        }
    }
?>

<!-- HTML code for deleting subject -->
<h2>Delete Subject</h2>
<p>Are you sure you want to delete this subject?</p>
<form method="post" action="">
    <input type="hidden" name="subject_id" value="<?php echo $subject_id; ?>">
    <input type="submit" name="delete" value="Yes">
    <a href="subjects.php">Cancel</a>
</form>

Last updated