PHP - How to Insert Data in Database using oops concept

Published June 26, 2021

This PHP example we will see how to insert data into MySQL database using oops concept. For this example we will create a class to handle Database connection and insert and retrieve data from database.

In oops concepts data will be handle inside objects of class.


View Demo


Define class

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({})


Let's create two php files one is for handle database connections and other is for user form to submit data to server

Database connection

define('DB_PASS' ,'');
define('DB_NAME', 'ebridge');
class DB_con
    function __construct()
        $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// 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 userdata(name,email,contactno,gender,education,addrss) values('$fname','$email','$contact','$gender','$education','$adrss')");
        return $ret;


User Form Data

$insertdata=new DB_con();
        echo "<script>alert('Data inserted');</script>";
        echo "<script>alert('Data not inserted');</script>";
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>RRTutors | DEMO</title>
<meta name="generator" content="Bootply" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="//"></script>
<link href="css/styles.css" rel="stylesheet">
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" rel="home" href="">RRTutors</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>


<div class="container-fluid">


<div class="col-sm-6">
<div class="row">
<div class="col-xs-12">
<h3>Insert Data In Databse using PHP OOPS Concept</h3>
<form name="insert" action="" method="post">
<table width="100%"  border="0">
<th width="26%" height="60" scope="row">Full Name :</th>
<td width="74%"><input type="text" name="fname" value="" class="form-control" required /></td>
<th height="60" scope="row">Email :</th>
<td><input type="email" name="email" value="" class="form-control" required /></td>
<th height="60" scope="row">Contact no. :</th>
<td><input type="text" name="contact" value="" maxlength="10" class="form-control" required /></td>
<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>
<th height="60" scope="row">Education :</th>
<td><select name="education" class="form-control">
<option value="">Select</option>
<option value="10th">11th</option>
<option value="10th">12th</option>
<option value="10th">BTech</option>
<option value="10th">Degree</option>
<option value="10th">Diplamo</option>
<option value="10th">MCA</option>
<option value="10th">MBA</option>
<option value="Graduate">Graduate</option>
</select> </td>
<th height="60" scope="row">Address :</th>
<td><textarea name="address" class="form-control">
<th height="60" scope="row">&nbsp;</th>
<td><input type="submit" value="Submit" name="submit" class="btn-primary" /></td>



<!-- script references -->
<script src="//"></script>
<script src="js/bootstrap.min.js"></script>


Upload these files into your server and replace database connection details in function class and run the index.php file


You can find the complete source code below



Article Contributed By :