How To Post Data using PDO in PHP/MySQL

0 2,615

Hi, Freinds in this post we are going to learn How To Post Data using PDO in PHP/MySQL. PDO is an acronym for PHP Data Objects. PDO is a lean, consistent way to access databases. This means developers can write portable code much easier. PDO is not an abstraction layer like PearDB. PDO is a more like a data access layer which uses a unified API.

How To Post Data using PDO in PHP/MySQL

How To Post Data using PDO in PHP/MySQL

1-Creating Database

  • Open PHPMyAdmin in your Browser
  • Click on Database Tab Display on Topside
  • Give the Database name “pdo”.
  • After Creating Database Open it.
  • Click on SQL Tab on Top area
  • Copy the Below Source Code and paste it.
  • Then Click on Go.
-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Feb 05, 2019 at 04:44 AM
-- Server version: 10.1.37-MariaDB
-- PHP Version: 5.6.39

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `pdo`
--

-- --------------------------------------------------------

--
-- Table structure for table `member`
--

CREATE TABLE `member` (
  `mem_id` int(11) NOT NULL,
  `firstname` varchar(50) NOT NULL,
  `lastname` varchar(50) NOT NULL,
  `age` int(10) NOT NULL,
  `address` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `member`
--

INSERT INTO `member` (`mem_id`, `firstname`, `lastname`, `age`, `address`) VALUES
(4, 'Priyanshu', 'Raj', 18, 'India'),
(5, 'Abhimanu', 'Kumar', 25, 'India'),
(6, 'Sonu', 'Kumar', 20, 'India');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `member`
--
ALTER TABLE `member`
  ADD PRIMARY KEY (`mem_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `member`
--
ALTER TABLE `member`
  MODIFY `mem_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

OR Import DB File

After Downloading the source code extract it in your root folder.

  • Open PHPMyAdmin in your Browser
  • Click on Database Tab Display on Topside
  • Give the Database name “pdo”.
  • After Creating Database Open it.
  • Click on Import Tab on Top area
  • You can Find Database folder in  Downloaded source code where find register.sql file then Select it.
  • Then Click on Go.

2- Creating Database Connection

After import Database File then next step is creating a database connection using php copy the below code and save it is as “conn.php”.

<?php
	$conn = new PDO( 'mysql:host=localhost;dbname=pdo', 'root', '');
	if(!$conn){
		die("Fatal Error: Connection Failed!");
	}
?>

3 – Creating Index Page

in this step we are creating page layout where you can add user and show all posted data. here the source code. you can copy the below code and save it as index.php.

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>How To Post Data using PDO in PHP/MySQL</title>
		<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
		<style type="text/css">
			body{
				margin-top:150px;
			}
			.well{
					-webkit-box-shadow: 1px 2px 11px 1px rgba(0,0,0,0.75);
					-moz-box-shadow: 1px 2px 11px 1px rgba(0,0,0,0.75);
					box-shadow: 1px 2px 11px 1px rgba(0,0,0,0.75);
					}
		</style>
	</head>
<body>

	<div class="col-md-3"></div>
	<div class="col-md-6 well">
		<h3 class="text-success">How To Post Data using PDO in PHP/MySQL</h3>
		<hr style="border-top:1px dotted #ccc;"/>
		<button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add User</button>
		<br /><br />
		<table class="table table-bordered">
			<thead class="alert-success">
				<tr>
					<th>Firstname</th>
					<th>Lastname</th>
					<th>Age</th>
					<th>Address</th>
				</tr>
			</thead>
			<tbody style="background-color:#fff;">
				<?php
						require_once 'conn.php';
	
						$sql = "SELECT * FROM `member`";
						$query = $conn->prepare($sql);
						$query->execute();
						
						while($fetch = $query->fetch()){
				?>
			
				<tr>
					<td><?php echo $fetch['firstname']?></td>
					<td><?php echo $fetch['lastname']?></td>
					<td><?php echo $fetch['age']?></td>
					<td><?php echo $fetch['address']?></td>
				</tr>
				
				<?php
					}
				?>
			</tbody>
		</table>
	</div>
	
	<div class="modal fade" id="form_modal" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<form method="POST" action="save_member.php">
					<div class="modal-header">
						<h3 class="modal-title">Add User</h3>
					</div>
					<div class="modal-body">
						<div class="col-md-2"></div>
						<div class="col-md-8">
							<div class="form-group">
								<label>Firstname</label>
								<input type="text" name="firstname" class="form-control" required="required"/>
							</div>
							<div class="form-group">
								<label>Lastname</label>
								<input type="text" name="lastname" class="form-control" required="required" />
							</div>
							<div class="form-group">
								<label>Age</label>
								<input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
							</div>
							<div class="form-group">
								<label>Address</label>
								<input type="text" name="address" class="form-control" required="required"/>
							</div>
						</div>
					</div>
					<div style="clear:both;"></div>
					<div class="modal-footer">
						<button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
						<button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
					</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	
	
<script src="js/jquery-3.2.1.min.js"></script>	
<script src="js/bootstrap.js"></script>	
</body>
</html>

4 -Post Data using PHP/MySQL with PDO

in this step we are going to post data in PHP into MySQL using PDO. Here the source code you can copy the below code and save it as save_member.php.

<?php
	require_once 'conn.php';
	
	if(ISSET($_POST['save'])){
	
		$firstname = $_POST['firstname'];
		$lastname = $_POST['lastname'];
		$age = $_POST['age'];
		$address = $_POST['address'];
	
		
		try{
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$sql = "INSERT INTO `member`(firstname, lastname, age, address)  VALUES ('$firstname', '$lastname', '$age', '$address')";
			$conn->exec($sql);
		}catch(PDOException $e){
			echo $e->getMessage();
		}
		
		$conn = null;
		
		header("location: index.php");
	
	}
?>

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

Download Source Code

Leave A Reply

Your email address will not be published.