PHP PHP OOPs Concepts

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

Recommended Tutorials for you


Related posts

Interfaces – the next level of abstraction

Anuj Kumar

How to fetch data in excel or generate excel file in PHP

Anuj Kumar

JQuery Ajax Tutorial – Learn About Developing Effective Next Generation Web Application

Anuj Kumar

Date And Time Formatting With PHP

Anuj Kumar

jQuery Dependent DropDown List – States and Districts

Anuj Kumar

PHP Constants

Anuj Kumar

Traits and code inclusion

Anuj Kumar

Adding Comments to PHP Code

Anuj Kumar

How to change Password in php

Anuj Kumar

12 comments

Sonam Singh November 10, 2018 at 6:31 am

I have go through lots of personal blogs together with completed many websites,however your work is undoubtedly brilliant.Caps off with your determination together with credibility.Do hold us advised in future as well.

Reply
sid patel November 10, 2018 at 6:31 am

Your content is very usefull for learning php
delete data in mysql using php class
edit data in mysql using php class

Reply
sid patel November 10, 2018 at 6:31 am

I want to this topic :
delete data in mysql using php class
edit data in mysql using php class

Reply
Anuj kumar November 10, 2018 at 6:42 am

I will post tutorial by tomorrow

Reply
rahul November 10, 2018 at 6:31 am

Saved as a favorite, I really like your web site!

Reply
Anuj kumar November 10, 2018 at 6:42 am

Thank you !

Reply
sid patel October 19, 2018 at 9:12 am

I want to this topic :
delete data in mysql using php class
edit data in mysql using php class

Reply
Anuj kumar October 24, 2018 at 6:17 pm

I will post tutorial by tomorrow

Reply
sid patel October 19, 2018 at 9:11 am

Your content is very usefull for learning php
delete data in mysql using php class
edit data in mysql using php class

Reply
Sonam Singh December 13, 2015 at 11:34 am

I have go through lots of personal blogs together with completed many websites,however your work is undoubtedly brilliant.Caps off with your determination together with credibility.Do hold us advised in future as well.

Reply
rahul November 3, 2015 at 6:08 pm

Saved as a favorite, I really like your web site!

Reply
Anuj kumar December 4, 2015 at 4:51 am

Thank you !

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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