In this tutorial we will create a Simple PDO CRUD using PHP. PHP is a server-side scripting language designed primarily for web development. PDO stands for PHP Data Objects. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.
Before we started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.
Creating Database
This is where we store all the data we process throug HTML forms. To create a database open any kind of database base server that you have(wamp, xamp, etc..). Then create database and name it 'db_member'. After that click SQL then copy/paste the code below then click GO.
Creating the database Connection
This is the where the database connection, just simple copy/paste the provided code below and save it as 'connection.php'.
<?php
$db_username = 'root';
$db_password = '';
$conn = new PDO( 'mysql:host=localhost;dbname=db_member', $db_username, $db_password );
if(!$conn){
die("Fatal Error: Connection Failed!"); }
?>
Creating The Mark UpThis is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as shown below. index.php
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - PDO CRUD</h3>
<hr style="border-top:1px dotted #ccc;" />
<div class="col-md-3"></div>
<div class="col-md-6">
<form method="POST" action="add.php">
<div class="form-group">
<label>Firstname</label>
<input class="form-control" type="text" name="firstname"/>
</div>
<div class="form-group">
<label>Lastname</label>
<input class="form-control" type="text" name="lastname"/>
</div>
<div class="form-group">
<label>Address</label>
<input class="form-control" type="text" name="address"/>
</div>
<div class="form-group">
<button class="btn btn-primary form-control" type="submit" name="save">Save</button>
</div>
</form>
</div>
<table class="table table-bordered">
<thead class="alert-danger">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody class="alert-warning">
<?php
require 'connection.php';
$sql = $conn->prepare("SELECT * FROM `member` ORDER BY `mem_id` DESC");
$sql->execute();
while($row = $sql->fetch()){
?>
<tr>
<td><?php echo $row['firstname']?></td>
<td><?php echo $row['lastname']?></td>
<td><?php echo $row['address']?></td>
<td><a href="edit.php?id=<?php echo $row['mem_id']?>">Edit</a> | <a href="delete.php?id=<?php echo $row['mem_id']?>">Delete</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</body>
</html>
edit.php
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - PDO CRUD</h3>
<hr style="border-top:1px dotted #ccc;" />
<div class="col-md-3"></div>
<div class="col-md-6">
<?php
require_once 'connection.php';
$id = $_GET['id'];
$sql = $conn->prepare("SELECT * FROM `member` WHERE `mem_id`='$id'");
$sql->execute();
$row = $sql->fetch();
}
?>
<form method="POST" action="update.php?id=<?php echo $id?>">
<div class="form-group">
<label>Firstname</label>
<input class="form-control" type="text" value="<?php echo $row['firstname']?>" name="firstname"/>
</div>
<div class="form-group">
<label>Lastname</label>
<input class="form-control" type="text" value="<?php echo $row['lastname']?>" name="lastname"/>
</div>
<div class="form-group">
<label>Address</label>
<input class="form-control" type="text" value="<?php echo $row['address']?>" name="address"/>
</div>
<div class="form-group">
<button class="btn btn-warning form-control" type="submit" name="update">Update</button>
</div>
</form>
<?php
$conn = null;
?>
</div>
</div>
</body>
</html>
Adding Function
We will now create the add function, this function will store the value into the database. To do that just copy/paste the code below then name it add.php.
<?php
require_once 'connection.php';
if(ISSET($_POST['save'])){ try{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `member` (`firstname`, `lastname`, `address`) VALUES ('$firstname', '$lastname', '$address')";
}catch(PDOException $e){
echo $e->getMessage();
}
$conn = null;
}
?>
Delete Function
We will now create the delete function, this will delete the targeted value base on the mem_id key. To do that just copy/paste the code below then name it delete.php.
<?php
require_once 'connection.php';
$id = $_GET['id'];
$sql = $conn->prepare("DELETE from `member` WHERE `mem_id`='$id'");
$sql->execute();
}
?>
Update Function
We will now create the update function, this code will stored all the value within the form and update the data into the database base on the mem_id. To do that just copy/paste the code below then name it update.php.
<?php
require_once 'connection.php';
if(ISSET($_POST['update'])){ try{
$id = $_GET['id'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE `member`SET `firstname` = '$firstname', `lastname` = '$lastname', `address` = '$address' WHERE `mem_id` = '$id'";
}catch(PDOException $e){
echo $e->getMessage();
}
$conn = null;
}
?>
There you have it we create a Simple PDO CRUD. I hope that this simple tutorial help your to your on working projects. For more updates and tutorials just kindly visit this site, don't forget to LIKE and SHARE. Enjoy Coding!!
Download