Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Follow publication

A Simple PHP (PDO) + MySQL CRUD Application Part #2

--

In our previous article, we covered the implementation of the add portion of PHP CRUD application. Let’s continue with the read, updateand delete portions by following the steps below;

7. Let’s create the front end for our edit operation by pasting the following code in our edit.php file. Here we fetch a student’s information based on their id and fill in their information into a form so they can edit it.

<?php require "db.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Students</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
</head>
<body>
<?php
$id = $_GET["id"];
$stmt = $conn->prepare("select * from student_info where id=:id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$response = $stmt->fetch();
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<a href="index.php">&larr; Back to Home Page</a>
</div>
</div>
<h1 class="text-center">Provide Student Details Here</h1>
<form action="update.php" method="post" class="center">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div>
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname" class="form-control" value="<?php echo $response['fname']; ?>">
</div><br>
<div>
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname" class="form-control" value="<?php echo $response['lname']; ?>">
</div><br>
<div>
<label for="dob">Date of Birth:</label>
<input type="date" name="dob" id="dob" class="form-control" value="<?php echo $response['dob']; ?>">
</div><br>
<div class="text-center">
<input type="submit" class="btn btn-lg btn-success" name="submit" type="submit">
</div>
</form>
</div>
</body>
</html>

8. For the backend of our edit operation let’s paste the following code in our update.php file. This will use the details from our edit.php file to update a student’s information.

<?php
require 'db.php';
$id = $_POST["id"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$dob = $_POST["dob"];
$stmt = $conn->prepare("update student_info set fname=:fname, lname=:lname, dob=:dob where id=:id;");
$stmt->bindParam(":fname", $fname);
$stmt->bindParam(":lname", $lname);
$stmt->bindParam(":dob", $dob);
$stmt->bindParam(":id", $id);
$stmt->execute();
header("Location: index.php");
?>

9. For our view single student operation, let’s paste the following code in our view.php file. This will basically fetch and display a student’s information.

<?php 
require "db.php";
try{
$stmt = $conn->prepare("select * from student_info;");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$students = $stmt->fetchAll();
}catch(Exception $e){
echo $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Students</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
</head>
<body>
<?php
$id = $_GET["id"];
$stmt = $conn->prepare("select * from student_info where id=:id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$response = $stmt->fetch();
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<a href="index.php">&larr; Back to Home Page</a>
</div>
</div>
<h1 class="text-center">Information on <?php echo $response["fname"]." ".$response["lname"]; ?></h1>
<table class="table">
<tr>
<th>
Property
</th>
<th>
Value
</th>
</tr>
<tr>
<td>First Name:</td>
<td><?php echo $response["fname"]; ?></td>
</tr>
<tr>
<td>Last Name:</td>
<td><?php echo $response["lname"]; ?></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><?php echo $response["dob"]; ?></td>
</tr>
</table>
</div>
</body>
</html>

10. Finally, let’s paste the following code in our delete.php file to conduct our delete operation. Given a student’s id it will delete the row representing them in the student_info table.

<?php
require "db.php";
try{
$id = $_POST["id"];
$stmt = $conn->prepare("delete from student_info where id=:id");
$stmt->bindParam(":id", $id);
$stmt->execute();
}catch(Exception $e){
$e->getMessage();
}
header("Location: index.php");
?>

Please follow my blog if you liked this article

Related Articles

A Simple PHP (PDO) + MySQL CRUD Application Part #1

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response