PHP-MySql Data Insert by OOPHP pattern

Hello Friends, Now we are going to learn how to insert data into mysql database by using object oriented php pattern. For this, I am making a database table called ‘datainsert’ and database name should be oophplearn.

Then we are going to write our first php function for Object Oriented in the file of ‘functions.class.php’. The code are in below-


class dataInsert{

public $command;

public function __construct(){
@mysql_connect (‘localhost’,’root’,”) or die(mysql_error());
@mysql_select_db(‘oophplearn’) or die(mysql_error());

public function insertData($value){

$sql = “INSERT INTO datainsert VALUES(”, ‘”.$value.”‘)”;

$this->command = @mysql_query($sql);

echo ‘Data Insert Successfully…’;

}//public function insertData($value){

} //class dataInsert{


Now I am writing another file called index.php into our directory. The code are in below-

//including the functions

$fun = new dataInsert();

//confiming work if submit button will be press…

//calling the function from class…
<!DOCTYPE html>
<title>Data Insert by Object Oriented PHP</title>

<form name=”” action=”” method=”post”>
Name: <input type=”text” name=”name” id=”name” placeholder=”Type You Name” required/>
<input type=”submit” name=”submit” value=”Save My Name”/>

Now, save all the works and run the index.php file by browser. Ok, if everything is okay, then you will get a from to write you name. Just write your name and submit your name. Then you will get a notification for confirmation of your post. Ok, now check in your database, and you will get your new information into your database. That’s enough for today.

Keep in touch and bye for today.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s