Home
MySQL
Learn PHP PDO CRUD using MySQL and Bootstrap
Learn PHP PDO CRUD using MySQL
and Bootstrap
December 28, 2016/ 0 Comments
Today in this tutorial we will see how to create PHP PDO CRUD
using MySQL and Bootstrap.PHP PDO its full form is PHP Data
Object.”PDO is a database access layer providing a uniform method
of access to multiple databases.”
Which databases PDO support?
PDO supports many of the popular databases as seen on the list
below.
DBLIB: FreeTDS / Microsoft SQL Server / Sybase
IBM (IBM DB2)
INFORMIX – IBM Informix Dynamic Server
MYSQL (http://www.mysql.com/): MySQL 3.x/4.0
OCI (http://www.oracle.com): Oracle Call Interface
ODBC: ODBC v3 (IBM DB2 and unixODBC)
PGSQL (http://www.postgresql.org/): PostgreSQL
SQLITE (http://sqlite.org/): SQLite 3.x
1-Create Database and Table
Open your phpmyadmin and run this SQL query in your SQL editor of
PHPMyAdmin.
CREATE DATABASE`pdocrud`;
CREATE TABLE `pdocrud`.`books` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 25 ) NOT NULL ,
`author` VARCHAR( 25 ) NOT NULL
) ENGINE = MYISAM ;
2-Db_con.php
Here in this file, we made a database connection with PHP code.
<?php
$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "pdocrud";
try
{
$DBcon = new
PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "ERROR : ".$e->getMessage();
}
3-Index.php
This is your main display file in which user can input value
using HTML form.
<?php
require_once 'crud.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<style type="text/css">
input
{
width:100%;
}
</style>
<link
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.c
ss" rel="stylesheet" integrity="sha384-
BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP PDO CRUD Application</title>
</head>
<body>
<div class="panel">
<form method="post">
<table border="1" width="40%" cellpadding="15" class="table table-
bordered">
<tr>
<td><input type="text" name="bname" placeholder="Book" required=""
value="<?php if(isset($_GET['edit_id'])){ print($editRow['name']); } ?>"
/></td>
</tr>
<tr>
<td><input type="text" name="bauthor" placeholder="Author" required=""
value="<?php if(isset($_GET['edit_id'])){ print($editRow['author']); }
?>" /></td>
</tr>
<tr>
<td>
<?php
if(isset($_GET['edit_id']))
{
?>
<button type="submit" name="update" class="btn btn-
warning">update</button>
<?php
}
else
{
?>
<button type="submit" name="save" class="btn btn-
success">save</button>
<?php
}
?>
</td>
</tr>
</table>
</form>
<br />
<?php
$stmt = $DBcon->prepare("SELECT * FROM books ORDER BY id DESC");
$stmt->execute();
?>
<table border="1" width="40%" class="table table-bordered">
<thead>
<tr>
<td>Book Name</td>
<td>Book Author</td>
<td>Update</td>
<td>Delete</td>
</tr>
</thead>
<?php
if($stmt->rowCount() > 0)
{
while($row=$stmt->FETCH(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php print($row['name']); ?></td>
<td><?php print($row['author']); ?></td>
<td><a onclick="return confirm('Sure to Edit ? ')"
href="index.php?edit_id=<?php print($row['id']); ?>">EDIT</a></td>
<td><a onclick="return confirm('Sure to Delete ? ')"
href="index.php?delete_id=<?php print($row['id']); ?>">DELETE</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td><?php print("nothing here..."); ?></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
4-Crud.php
In this PHP separate file, all edit delete and insert operations will occur.
<?php
require_once 'db_con.php';
if(isset($_POST['save']))
{
$bname= $_POST['bname'];
$bauthor = $_POST['bauthor'];
$stmt = $DBcon->prepare("INSERT INTO books(name,author) VALUES(:bname,
:bauthor)");
$stmt->bindparam(':bname', $bname);
$stmt->bindparam(':bauthor', $bauthor);
$stmt->execute();
if(isset($_GET['delete_id']))
{
$id = $_GET['delete_id'];
$stmt = $DBcon->prepare("DELETE FROM books WHERE id=:id");
$stmt->execute(array(':id' => $id));
header("Location: index.php");
}
if(isset($_GET['edit_id']))
{
$stmt = $DBcon->prepare("SELECT * FROM books WHERE id=:id");
$stmt->execute(array(':id' => $_GET['edit_id']));
$editRow=$stmt->FETCH(PDO::FETCH_ASSOC);
if(isset($_POST['update']))
{
$bname = $_POST['bname'];
$bauthor = $_POST['bauthor'];
$id = $_GET['edit_id'];
$stmt = $DBcon->prepare("UPDATE books SET name=:bname, author=:bauthor
WHERE id=:id");
$stmt->bindparam(':bname', $bname);
$stmt->bindparam(':bauthor', $bauthor);
$stmt->bindparam(':id', $id);
$stmt->execute();
header("Location: index.php");
}
?>
5-Conclusion
So today in this interesting PHP Data Object topic we have learned PHP PDO CRUD with
MySQL database.If you find this information useful, remember to share it on your social
network. If you have any specific concerns/doubt about this article you can comment below
and let us know.