CRUD code for subjects
here's an example of how to create a Subject CRUD system in PHP and MySQL:
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;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>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');
}
?>Create a page to view all subjects:
Create a form to edit a subject:
Delete Subject:
Last updated