How to Insert Data in Database using PHP OOPS

How to Insert Data in Database using PHP OOPS

In this Post We Will Explain About is How to Insert Data in Database using PHP OOPS With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to how to fetch data from database in php using oops Example

In this post we will show you Best way to implement php mysql insert operation in PHP using OOP, hear for insert data in mysql using php class with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read :  Installing laravel on windows wamp xampp

Setp 1.First create a html form

Index.php

	<form name="insert" action="" method="post">
       <table width="100%"  border="0">
  <tr>
    <th width="26%" height="50" scope="row users">USER Full Name :</th>
    <td width="74%"><input type="text" name="user_fname" value="" class="form-control" required /></td>
  </tr>
  <tr>
    <th height="50" scope="row users">USER Email :</th>
    <td><input type="email" name="email" value="" class="form-control" required /></td>
  </tr>
  <tr>
    <th height="50" scope="row users">USER Contact no. :</th>
    <td><input type="text" name="user_mobile" value="" class="form-control" required /></td>
  </tr>
  <tr>
    <th height="50" scope="row users">USER Gender :</th>
    <td><input type="radio" name="user_gender" value="Male" required /> Male   
	<input type="radio" name="user_gender" value="Female" required /> Female</td>
  </tr>
   <tr>
    <th height="50" scope="row users">USER Education :</th>
    <td><select name="user_edu" class="form-control">
	<option value="">Select Any one</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="50" scope="row users">USER Address :</th>
    <td><textarea name="user_address" class="form-control">
	</textarea></td>
  </tr>
  <tr>
    <th height="50" scope="row users"> </th>
    <td><input type="submit" value="Submit" name="submit" class="btn-primary" /></td>
  </tr>
</table>
 
     </form>

Setp 2. Create a database with name demos

Inside demos content data base make a table inserdata
Sample some Structure of user_mst table

CREATE TABLE IF NOT EXISTS `user_mst` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`phone` bigint(20) NOT NULL,
`user_gender` varchar(255) NOT NULL,
`user_edu` varchar(255) NOT NULL,
`user_user_address` longtext NOT NULL,
`posting_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

Setp 3. Create a php page function .php

Inside this simple page a using class DB_concreate or define. Db_con class contain simple database connection and some insert funtion.

function .php

<?php
session_start();
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'demos');
class DB_con
{
	function __construct()
	{
		$conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die('localhost connection problem'.mysql_error());
		mysql_select_db(DB_NAME, $conn);
	}
	public function insert($user_fname,$email,$user_mobile,$user_gender,$user_edu,$adrss)
	{
	$ret=mysql_query("insert into user_mst(name,email,phone,user_gender,user_edu,user_user_address) values('$user_fname','$email','$user_mobile','$user_gender','$user_edu','$adrss')");
	return $ret;
	}
}
?>

Setp 4.Define a oject and class the insert funtion

Put this source code at the top of the simple root file index.php page

<?php
include_once("function.php");
$user_mst=new DB_con();
if(isset($_POST['submit']))
{
	$user_fname=$_POST['user_fname'];
	$email=$_POST['email'];
	$user_mobile=$_POST['user_mobile'];
	$user_gender=$_POST['user_gender'];
	$user_edu=$_POST['user_edu'];
	$adrss=$_POST['user_address'];
	$sql=$user_mst->insert($user_fname,$email,$user_mobile,$user_gender,$user_edu,$adrss);
	if($sql)
	{
	echo "<script>alert('USER Data inserted');</script>";
	}
	else
	{
	echo "<script>alert('USER Data not inserted');</script>";
	}
}
 ?>

Example

I hope you have Got What is Objects Orientation in PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *