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

<?php
session_start();
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'ebridge');
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 userdata(name,email,contactno,gender,education,addrss) values('$fname','$email','$contact','$gender','$education','$adrss')");
        return $ret;
    }
}
?>

 

User Form Data

<?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>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<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="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="css/styles.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" rel="home" href="https://rrtutors.com/">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>
</button>
</div>

</nav>

<div class="container-fluid">


<!--/left-->

<!--center-->
<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">
<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" value="" maxlength="10" 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">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>
</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>

</div>
</div>
<hr>


</div><!--/center-->
</div><!--/container-fluid-->
<!-- script references -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

 

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 :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1140 Views