PHP PDO CRUD Operations

Sujith Sandeep
4 min readAug 20, 2022

--

PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. PDO will work on 12 different data systems. Only a few queries and connections are needed to be changed if you need to change the database.

CRUD

CRUD refers to the operations that create, read, update and delete. Follow the instructions below to create a PDO CRUD application.

Now, let’s do some coding.

Database Configuration

Initially, we are going to create a database and a table and we are going to do the CURD operation for users in this article.

User Table

config.php

<?php
session_start();
// Define database
define('dbhost', 'localhost');
define('dbuser', 'root');
define('dbpass', '');
define('dbname', 'pdocrud');
// Connecting database
try {
$connect = new PDO("mysql:host=" . dbhost . "; dbname=" . dbname, dbuser, dbpass);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>

Layout setup

Since we are going to write a few things that are common in many files. Instead of writing the code in all the files, we write it in a common file and include it wherever it is needed.

header.php

<?php
include 'config.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>Document</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<style>
.heading {
text-align: center;
padding-top: 4%;
padding-bottom: 4%;
}
.createbtn {
float: right;
padding-bottom: 2%;
}
</style>
</head>
<body class="container">
<h3 class="heading">PDO CRUD OPERATIONS</h3>

CRUD Operations

index.php

<?php
include 'header.php';
$stmt = $connect->prepare('SELECT * FROM users');
$stmt->execute();
?>
<a href="createuser.php" class="createbtn"><button type="button" class="btn btn-primary">Add User</button></a>
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Email Id</th>
<th scope="col">Mobile No</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $data['name']; ?></td>
<td><?php echo $data['email_id']; ?></td>
<td><?php echo $data['mobile_no']; ?></td>
<td>
<?php
echo '<a href="edituser.php?id='.$data['id'].'"><button type="button" class="btn btn-primary">Edit</button></a>&nbsp;&nbsp';
echo '<a href="deleteuser.php?id='.$data['id'].'"><button type="button" class="btn btn-danger">Delete</button></a>&nbsp;&nbsp';
?>
</td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
</body>
</html>

createuser.php

<?php
include 'header.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt = $connect->prepare('INSERT into users (name, email_id, mobile_no) VALUES (:name, :emailid, :mobileno)');
$stmt->execute(array(
':name' => $_POST['name'],
':emailid' => $_POST['emailid'],
':mobileno' => $_POST['mobileno'],
));
header("Location: /pdocrud");
}
?>
<form method="POST" action="#">
<div class="col-md-6">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Name">
</div>
<br />
<div class="form-group">
<label for="emailid">Email address</label>
<input type="email" name="emailid" class="form-control" id="emailid" aria-describedby="emailHelp" placeholder="Enter email id">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<br />
<div class="form-group">
<label for="mobileno">Mobile No</label>
<input type="number" name="mobileno" class="form-control" id="mobileno" placeholder="Mobile No">
</div>
<br />
<button type="submit" class="btn btn-primary">Submit</button>&nbsp;&nbsp;&nbsp;
<a href="/pdocrud"><button type="button" class="btn btn-danger">Exit</button></a>
</div>
</form>
</body>
</html>

edituser.php

<?php
include 'header.php';
if(!isset($_GET['id']) || empty($_GET['id'])) {
header("Location: /pdocrud");
} else {
$stmt = $connect->prepare('SELECT * from users where id = :id');
$stmt->execute(array(
':id' => $_GET["id"]
));
$userData = $stmt->fetch(PDO::FETCH_ASSOC);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt = $connect->prepare('UPDATE users set name = :name, email_id = :emailid, mobile_no = :mobileno where id = :id');
$stmt->execute(array(
':name' => $_POST['name'],
':emailid' => $_POST['emailid'],
':mobileno' => $_POST['mobileno'],
':id' => $_GET['id'],
));
header("Location: /pdocrud");
}
?>
<form method="POST" action="#">
<div class="col-md-6">
<div class="form-group">
<label for="name">Name</label>
<input type="text" value="<?php echo $userData['name']; ?>" name="name" class="form-control" id="name" placeholder="Name">
</div>
<br />
<div class="form-group">
<label for="emailid">Email address</label>
<input type="email" value="<?php echo $userData['email_id']; ?>" name="emailid" class="form-control" id="emailid" aria-describedby="emailHelp" placeholder="Enter email id">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<br />
<div class="form-group">
<label for="mobileno">Mobile No</label>
<input type="number" value="<?php echo $userData['mobile_no']; ?>" name="mobileno" class="form-control" id="mobileno" placeholder="Mobile No">
</div>
<br />
<button type="submit" class="btn btn-primary">Submit</button>&nbsp;&nbsp;&nbsp;
<a href="/pdocrud"><button type="button" class="btn btn-danger">Exit</button></a>
</div>
</form>
</body>
</html>

deleteuser.php

<?php
include 'config.php';
if(!isset($_GET['id']) || empty($_GET['id'])) {
header("Location: /pdocrud");
} else {
$stmt = $connect->prepare('DELETE from users where id = :id');
$stmt->execute(array(
':id' => $_GET["id"]
));
header("Location: /pdocrud");
}
?>

--

--