Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
PHP CRUD Operation using PDO Extension

php crud operation using pdo extension

Posted on June 4, 2018December 17, 2022 By XpertPhp

In this article, We will create a PHP crud operation using PDO extension. The CRUD means Create, edit, update and delete. so here we will give you a simple example of insert, update, delete using PHP PDO with MySQL.

PDO stands for PHP Data Objects. PDO is a simple and consistent way to access databases. This means that developers can write portable code much more easily. PDO is not an abstraction layer like PearDB. PDO is just more like a data access layer that uses a unified API.

Read Also:
PHP CRUD operation with MySQL Example

So you can see our example for PHP crud operation using PDO extension.

Create Database and Tables

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(64) NOT NULL,
  `last_name` varchar(64) NOT NULL,
  `address` text NOT NULL,
  `email` varchar(64) NOT NULL,
  `mobile` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
 
 

connect to database in mysql

Create a  new connect.php file. The code below is to provide how to connect PDO with MySQL.
The following code is used to connect MySQL from PHP. It requires hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
// DB credentials.
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'php_crud_pdo';
try
{
$db = new PDO("mysql:host=".$db_host.";dbname=".$db_name,$db_user, $db_pass);
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>

Creating New Record in MySQL Database

Create a PHP file add.php in this file in add the below code.
The code below in the INSERT query through creates a new record to the database table. This HTML form contains input fields to enter user data to be inserted into the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
if(isset($_POST['btnadd']))
{
    $first_name  = $_POST['txtFname'];
    $last_name   = $_POST['txtLname'];
    $address     = $_POST['txtAddress'];
    $email       = $_POST['txtEmail'];
    $mobile      = $_POST['txtMobile'];
 
    include('connect.php');
$sql="insert into users (first_name,last_name,address,email,mobile) values(:first_name,:last_name,:address,:email,:mobile)";
$query = $db->prepare($sql);
$query->bindParam(':first_name',$first_name,PDO::PARAM_STR);
$query->bindParam(':last_name',$last_name,PDO::PARAM_STR);
$query->bindParam(':address',$address,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':mobile',$mobile,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $db->lastInsertId();
    header('location:index.php');
 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="row" style="margin-bottom:10px;">
<div class="col-lg-10"></div>
<div class="col-lg-2"><a href="index.php" class="btn btn-primary">Back</a></div>
</div>
<form method="post" name="frmAdd">
  <div class="form-group">
<label for="txtFname">First Name:</label>
<input type="text" class="form-control" name="txtFname" required  id="txtFname">
  </div>
  <div class="form-group">
<label for="txtLname">Last Name:</label>
<input type="text" class="form-control" required name="txtLname" id="txtLname">
  </div>
   <div class="form-group">
<label for="txtEmail">Email:</label>
<input type="email" class="form-control" required name="txtEmail" id="txtEmail">
  </div>
  <div class="form-group">
<label for="txtMobile">Mobile:</label>
<input type="text" class="form-control" required name="txtMobile" id="txtMobile">
  </div>
  <div class="form-group">
<label for="txtAddress">Address:</label>
<textarea name="txtAddress" class="form-control" required id="txtAddress"></textarea>
  </div>
  <button type="submit" class="btn btn-success" name="btnadd">Add</button>
</form>
</div>
</body>
</html>

PHP MySQL Read

Create a new PHP file index.php in this file in add the below code.
The following code shows how to fetch all the records from the database in the index file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="row" style="margin-bottom:10px;">
<div class="col-lg-10"></div>
<div class="col-lg-2"><a href="add.php" class="btn btn-primary">Add</a></div>
</div>
<table class='display dataTable table table-bordered table-striped  no-footer' align="center">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Email</th>
<th>Mobile</th>
<th>Action</th>
</tr>
</thead>
<?php
include('connect.php');
$slt="select * from users";
$query=$db->prepare($slt);
$query->execute();
while($row=$query->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['mobile']; ?></td>
<td><a class="btn btn-primary" href="edit.php?edit_id=<?php echo $row['id'];?>">Edit</a>&nbsp;&nbsp;<a class="btn btn-danger" href="delete.php?delete_id=<?php echo $row['id'];?>" onclick="return confirm('Are you sure you want to delete this data?');">Delete</a> </td>
</tr>
<?php
}
?>
  
</table>
</div>
</body>
</html>

Php MySQL Update Record

Create a new edit.php file in this file to add the below code.
First, we will fetch a particular data record in the update form. On submitting edited user information we form an update query to edit the record with the reference of its edit_id.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
$edit_id = $_REQUEST['edit_id'];
include('connect.php');
$slt="select * from users where id=:id";
$query=$db->prepare($slt);
$query->bindParam(':id',$edit_id,PDO::PARAM_STR);
$query->execute();
$row=$query->fetch(PDO::FETCH_ASSOC);
?>
<?php
if(isset($_POST['btnUpdate']))
{
    $first_name  = $_POST['txtFname'];
    $last_name   = $_POST['txtLname'];
    $address     = $_POST['txtAddress'];
    $email       = $_POST['txtEmail'];
    $mobile      = $_POST['txtMobile'];
 
    include('connect.php');
    $sql="update users set first_name=:first_name,last_name=:last_name,address=:address,email=:email,mobile=:mobile where id=:id";
$query = $db->prepare($sql);
 
$query->bindParam(':first_name',$first_name,PDO::PARAM_STR);
$query->bindParam(':last_name',$last_name,PDO::PARAM_STR);
$query->bindParam(':address',$address,PDO::PARAM_STR);
$query->bindParam(':email',$email,PDO::PARAM_STR);
$query->bindParam(':mobile',$mobile,PDO::PARAM_STR);
$query->bindParam(':id',$edit_id,PDO::PARAM_STR);
$query->execute();
    header('location:index.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
</head>
<body>
<div class="container">
<div class="row" style="margin-bottom:10px;">
<div class="col-lg-10"></div>
<div class="col-lg-2"><a href="index.php" class="btn btn-primary">Back</a></div>
</div>
<form method="post" name="frmUpdate">
  <div class="form-group">
<label for="txtFname">First Name:</label>
<input type="text" class="form-control" name="txtFname" required id="txtFname" value="<?php echo $row['first_name'];?>">
  </div>
  <div class="form-group">
<label for="txtLname">Last Name:</label>
<input type="text" class="form-control" name="txtLname" required id="txtLname" value="<?php echo $row['last_name'];?>">
  </div>
   <div class="form-group">
<label for="txtEmail">Email:</label>
<input type="email" class="form-control" name="txtEmail" required id="txtEmail" value="<?php echo $row['email'];?>">
  </div>
  <div class="form-group">
<label for="txtMobile">Mobile:</label>
<input type="text" class="form-control" name="txtMobile" required id="txtMobile" value="<?php echo $row['mobile'];?>">
  </div>
  <div class="form-group">
<label for="txtAddress">Address:</label>
<textarea name="txtAddress" class="form-control" required id="txtAddress"><?php echo $row['address'];?></textarea>
  </div>
  <button type="submit" class="btn btn-success" value="" name="btnUpdate">Update</button>
</form>
</div>
</body>
</html>

Delete Record from MySQL Database

Create a new delete.php file in this file to add the below code.
The below code is used to delete a particular record from the database.

1
2
3
4
5
6
7
8
<?php
include('connect.php');
$sql = "delete from users WHERE id=:id";
$query = $db->prepare($sql);
$query->bindParam(':id',$_REQUEST['delete_id'], PDO::PARAM_STR);
$query->execute();
header("location:index.php");
?>

We think would you like this article, so you can click on “Show Demo” button and you can see this demo article.

Show Demo

Download

Php, MySql

Post navigation

Previous Post: How to create Pagination in Codeigniter and MySql
Next Post: Autocomplete Textbox in CodeIgniter using Typeahead

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com