10.PHP Create, Read, Update, Delete (CRUD)
10.PHP Create, Read, Update, Delete (CRUD)
Programming
Create, Read, Update, Delete using PHP and MySQL
What is CRUD?
CRUD stands for:
- Create
- Read
- Update
- Delete
A usual standard term for managing data in every server side application.
Parts of CRUD
1. List of all records
2. Form to add record
3. Form to edit record
4. Delete record confirmation
5. Form to view record (only read, might need or not)
Preparation
1. Need to create table for Majors
2. Need to create table for Students
Files list:
3. Styling: style.css
4. Header with Bootstrap: header.php
5. Navigation: nav.php
6. Database logic: database.php
7. List of students: index.php
8. List of majors: listMajor.php
9. Student Form for create and update: studentForm.php
10.Major Form for create and update: majorForm.php
11.Deletion confirmation: deleteStudent.php and deleteMajor.php
Table for Major
We have:
<nav class="nav">
<a class="nav-link" href="index.php">Students</a>
<a class="nav-link" href="listMajor.php">Majors</a>
</nav>
Main file for students: index.php
<!DOCTYPE html>
<html lang="en">
<?php include_once("header.php"); ?>
<body>
<?php include_once("nav.php"); ?>
<div class="container mt-3">
<table class="table table-sm table-bordered">
<tr>
<th>Name</th>
<th>NIM</th>
<th>Phone</th>
<th>Major</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
require("database.php");
$db = new DBConnection();
$students = $db->getAllStudents();
Main file for students: index.php (continue)
foreach ($students as $row) {
$major = $db->getMajorById($row['major_id'])->fetch();
?>
<tr>
<td><?= $row["name"]; ?></td>
<td><?= $row["nim"]; ?></td>
<td><?= $row["phone"]; ?></td>
<td><?= $major["name"] ?></td>
<td>
<a class="btn btn-warning" href="studentForm.php?id=<?= $row['id']; ?>">Edit</a>
</td>
<td>
<a class="btn btn-danger" href="deleteStudent.php?id=<?= $row['id']; ?>">Delete</a>
</td>
</tr>
<?php
}
?>
</table>
<div>
<a class="btn btn-success" href="studentForm.php">Add New Data</a>
</div>
</div>
</body>
</html>
List of Students result
Main file for major: listMajor.php
<!DOCTYPE html>
<html lang="en">
<?php include_once("header.php"); ?>
<body>
<?php
require("database.php");
include_once("nav.php");
$db = new DBConnection();
$majors = $db->getAllMajors();
?>
<div class="container mt-3">
<table class="table table-sm table-bordered">
<tr>
<th>Code</th>
<th>Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
Main file for major: listMajor.php (continuation)
<?php
// loop all majors and show in table
foreach ($majors as $row) {
?>
<tr>
<td><?= $row["code"] ?></td>
<td><?= $row["name"] ?></td>
<td>
<a class="btn btn-warning" href="majorForm.php?id=<?= $row['id'] ?>">Edit</a>
</td>
<td>
<a class="btn btn-danger" href="deleteMajor.php?id=<?= $row['id']?>">Delete</a>
</td>
</tr>
<?php
}
?>
</table>
<div>
<a class="btn btn-success" href="majorForm.php">Add New Data</a>
</div>
</div>
</body>
</html>
List of Majors result
Deletion part: deleteStudent.php
<?php
require_once("header.php");
require_once("database.php");
$db = new DBConnection();
if (isset($_POST["submit"]) and isset($_GET["id"])) {
$db->deleteStudent($_GET["id"]);
header("Location: index.php");
}
?>
<div class="container">
<form action="" method="post">
Are you sure you want to delete this data?<br/>
<input class="mt-3 btn btn-danger" type="submit" name="submit" value="Confirm"/>
<a class="mt-3 btn btn-info" href="listMajor.php">Cancel</a>
</form>
</div>
Deletion part: deleteMajor.php
<?php
require_once("header.php");
require_once("database.php");
$db = new DBConnection();
if (isset($_POST["submit"]) and isset($_GET["id"])) {
$db->deleteMajor($_GET["id"]);
header("Location: listMajor.php");
}
?>
<div class="container">
<form action="" method="post">
Are you sure you want to delete this data?<br/>
<input class="mt-3 btn btn-danger" type="submit" name="submit" value="Confirm"/>
<a class="mt-3 btn btn-info" href="listMajor.php">Cancel</a>
</form>
</div>
Deletion page result
Main part: The Forms
We have 2 forms:
1. Student form
2. Major form
Form: studentForm.php (part 1)
<?php
/**
* Check submit button, take data and save to DB
*/
require_once("header.php");
require_once("database.php");
$db = new DBConnection();
$errors = [];
$majors = $db->getAllMajors()->fetchAll();
if (empty($_POST["nim"])) {
array_push($errors, "NIM is required");
} else {
if (strlen($_POST["nim"]) != 12) {
array_push($errors, "NIM must be exactly 12 characters");
}
}
if (count($errors) == 0) {
if (isset($_GET["id"])) {
$db->updateStudent($_GET["id"], $name, $nim, $phone, $major);
} else {
$db->addStudent($name, $nim, $phone, $major);
}
header("Location: index.php");
}
}
?>
Form: studentForm.php (part 3)
<div class="container mt-4">
<form class="form" action="" method="post">
<div class="row mt-3">
<div class="col-4">
<label for="name">Student Name</label>
<input class="form-control" type="text" name="name"
value="<?php echo($name); ?>" placeholder="Enter Student Name"/>
</div>
</div>
<div class="row mt-3">
<div class="col-4">
<label for="nim">Student NIM</label>
<input class="form-control" type="text" name="nim"
value="<?php echo($nim); ?>" placeholder="Enter Student NIM"/>
</div>
</div>
<div class="row mt-3">
<div class="col-4">
<label for="phone">Student Phone</label>
<input class="form-control" type="text" name="phone"
value="<?php echo($phone); ?>"placeholder="Enter Student Phone"/>
</div>
</div>
Form: studentForm.php (part 4)
<div class="row mt-3">
<div class="col-4">
<label for="major">Student Major</label>
<select class="form-select" name="major">
<?php
// NOTE: if the major == $row["id"], put `selected` attribute
foreach ($majors as $row) {
$selected = $row["id"] == $major ? "selected" : "";
echo("<option value=\"{$row['id']}\" {$selected}>"); ← Notice this
echo("{$row['code']} - {$row['name']}");
echo("</option>");
part
}
?>
</select>
</div>
</div>
<input class="mt-3 btn btn-primary" type="submit" name="submit" value="Add Record"/>
<a class="mt-3 btn btn-warning" href="index.php">Cancel</a>
</form>
<div class="mt-4">
<ul class="error">
<?php
foreach ($errors as $err) {
echo("<li>{$err}</li>");
}
?>
</ul>
</div>
</div>
Student Form Result
Form: majorForm.php (part 1)
<?php
/**
* Check submit button, take data and save to DB
*/
require_once("header.php");
require_once("database.php");
$db = new DBConnection();
$errors = [];
if (empty($_POST["code"])) {
array_push($errors, "Code is required");
} else {
if (strlen($_POST["code"]) != 3) {
array_push($errors, "Code must be exactly 3 characters");
}
}
Faculty has:
1. ID as primary key
2. Name
3. Code
4. Location
https://www.freecodecamp.org/news/crud-operations-explained/
https://github.com/williempu/php-crud