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-

<?php

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);

if($this->command){
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-

<?php
//including the functions
require_once(‘functions.class.php’);

$fun = new dataInsert();

//confiming work if submit button will be press…
if(isset($_POST[‘submit’])){

//calling the function from class…
$fun->insertData($_POST[‘name’]);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Insert by Object Oriented PHP</title>
</head>

<body>
<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”/>
</form>
</body>
</html>

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.

Advertisements

3 Comments

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s