How to Insert data in Database Using PHP PDO, Mysql

Written by admin

The SQL code for the student_details table:

CREATE TABLE `student_details` (
  `id` int(11) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `contact_no` varchar(45) DEFAULT NULL,
  `gender` varchar(45) DEFAULT NULL,
  `qualification` varchar(45) DEFAULT NULL,
  `address` varchar(450) DEFAULT NULL,
  `create_date` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Here we are utilizing PDO Object-Oriented strategy to Open a Connection to MySQL.
This code for the database connection file.

servername=localhost
Username=root
Password=""
Databasename='student'
dbconnection.php
<?php 
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','student');
// Establish database connection.
try
{
$dbconnection = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e)
{
exit("Error: " . $e->getMessage());
}
?>
insert.php
<?php 
include  'dbconnection.php';
$succMsg= NULL;
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$contactno = $_POST['contactno'];
$gender = $_POST['gender'];
$qualification = $_POST['qualification'];
$address = $_POST['address'];

$sql="INSERT INTO student_details (name,email,contact_no,gender,qualification, address) Values(:names,:emailid,:mobileno,:gendr,:qualifications,:addresss)";
$query = $dbconnection -> prepare($sql);
$query->bindParam(':names',$name,PDO::PARAM_STR);
$query->bindParam(':emailid',$email,PDO::PARAM_STR);
$query->bindParam(':mobileno',$contactno,PDO::PARAM_STR);
$query->bindParam(':gendr',$gender,PDO::PARAM_STR);
$query->bindParam(':qualifications',$qualification,PDO::PARAM_STR);
$query->bindParam(':addresss',$address,PDO::PARAM_STR);
$query -> execute();
$lastInsertId = $dbconnection->lastInsertId();
if($lastInsertId>0)
{
echo "Data insert Successfully";
 }
else {

echo "Data not insert successfully";
 }
}
?>
index.php
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<div class="container">
        <div class="row centered-form">
        <div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
        	<div class="panel panel-default">
        		<div class="panel-heading">
			    		<h3 class="panel-title">Insert data in Database Using PHP <small><a href="http://thephpconcept.com/">The PHP Concept</a></small></h3>
			 			</div>
			 			<div class="panel-body">
			    		<form method="POST" action="insert.php">
			    			
                            <div class="form-group">
			    				<label for="name">Name</label>
			    				<input type="text" name="name" id="name" class="form-control input-sm" placeholder="Name" required="">
			    			</div>
			    			<div class="form-group">
			    				<label for="email">Email</label>
			    				<input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email Address" required="">
			    			</div>
			    			<div class="form-group">
			    				<label for="contactno">Contact No</label>
			    				<input type="text" name="contactno" id="contactno" class="form-control input-sm" placeholder="Contact No" required="">
			    			</div>
			    			<div class="form-group">
			    				<label for="name">Gender</label>
			    				<input type="radio" name="gender" value="Male">Male<input type="radio" name="gender" value="Female">Female
			    			</div>
			    			<div class="form-group">
			    				<label for="qualification">Qualification</label>
			    				<select name="qualification" id="qualification" class="form-control input-sm" placeholder="Qualification" required="">
			    					<option value="NA">--select--</option>
			    					<option value="10th">10th</option>
			    					<option value="12th">12th</option>
			    					<option value="Graduation ">Graduation</option>
			    				</select>
			    			</div>
			    			<div class="form-group">
			    				<label for="name">Address</label>
			    				<textarea name="address" id="address" class="form-control input-sm" required=""></textarea>
			    			</div>
                               <input type="submit" name="submit" value="submit" class="btn btn-info btn-block">
			    		
			    		</form>
			    	</div>
	    		</div>
    		</div>
    	</div>
    </div>
    <style type="text/css">
    	body{
    background-color: #fff;
}
.centered-form{
	margin-top: 60px;
}

.centered-form .panel{
	background: rgba(255, 255, 255, 0.8);
	box-shadow: rgba(0, 0, 0, 0.3) 20px 20px 20px;
}
    </style>
How to Insert data in Database Using PHP PDO, MySQL With Source Code
Size: 5kb
Version: 2.0

About the author

admin

Hi there! I'm Shiv Gupta. I specialized in building Websites. I write blogs in my free time. I really like to find out and share my knowledge with others.I founded ThePhpConcept in March 2020. I started this blog in order that I can interact with some like-minded people and also help people learning PHP, Mysql, jQuery, Html, and PHP Projects and related technologies.
Support-thephpconcept@gmail.com

1 Comment

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.