PHPGurukul

How to Insert Data in Database using PHP OOPS Concept

In this post I will explain how to insert data in database using oops concept.
View Demo

1.First create a html form
Index.php

<form name="insert" action="" method="post">
<table width="100%"  border="0">
<tr>
<th width="26%" height="60" scope="row">Full Name :</th>
<td width="74%"><input type="text" name="fname" value="" class="form-control" required /></td>
</tr>
<tr>
<th height="60" scope="row">Email :</th>
<td><input type="email" name="email" value="" class="form-control" required /></td>
</tr>
<tr>
<th height="60" scope="row">Contact no. :</th>
<td><input type="text" name="contact" maxlength="10" value="" class="form-control" required /></td>
</tr>
<tr>
<th height="60" scope="row">Gender :</th>
<td><input type="radio" name="gender" value="Male" required /> Male  &nbsp;
<input type="radio" name="gender" value="Female" required /> Female</td>
</tr>
<tr>
<th height="60" scope="row">Education :</th>
<td><select name="education" class="form-control">
<option value="">Select</option>
<option value="10th">10th</option>
<option value="12th">12th</option>
<option value="Graduate">Graduate</option>
<option value="Post Graduate">Post Graduate</option>
</select> </td>
</tr>
<tr>
<th height="60" scope="row">Address :</th>
<td><textarea name="address" class="form-control">
</textarea></td>
</tr>
<tr>
<th height="60" scope="row">&nbsp;</th>
<td><input type="submit" value="Submit" name="submit" class="btn-primary" /></td>
</tr>
</table>
</form>

2. Create a database with name demos
Inside demos data base create a table inserdata
Sample Structure of insertdata table

CREATE TABLE IF NOT EXISTS `insertdata` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`contactno` bigint(20) NOT NULL,
`gender` varchar(255) NOT NULL,
`education` varchar(255) NOT NULL,
`addrss` longtext NOT NULL,
`posting_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

3. Create a php page function .php 
Inside this page a class DB_con define. Db_con class contain db connection and insert funtion.
function .php

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'demos');
class DB_con
{
function __construct()
{
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
$this->dbh=$con;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
public function insert($fname,$email,$contact,$gender,$education,$adrss)
{
$ret=mysqli_query($this->dbh,"insert into insertdata(name,email,contactno,gender,education,addrss) values('$fname','$email','$contact','$gender','$education','$adrss')");
return $ret;
}
}
?>

4.Define a object and class the insert function
Put this code at the top of the index.php page

<?php
include_once("function.php");
$insertdata=new DB_con();
if(isset($_POST['submit']))
{
$fname=$_POST['fname'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$gender=$_POST['gender'];
$education=$_POST['education'];
$adrss=$_POST['address'];
$sql=$insertdata->insert($fname,$email,$contact,$gender,$education,$adrss);
if($sql)
{
echo "<script>alert('Data inserted');</script>";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}
 ?>
View Demo
Download Full source code(Insert Data Using OOPs Concept)
Size: 28 KB
Version: V 1.2

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


13 comments

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

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.