How to Insert Data in Database using PHP OOPS Classes and Objects

Insert Data into database using classes and object.
Create database and table into you localhost server ( phpmyadmin )
php oops concept

Add fields that you require to insert, i'm doing here users related information name, email, contact, gender, education, address. When you done with table we can work on next step.
Create a form inside users.php file where we can get information from user to insert into database.

Full Name :
Email :
Contact no. :
Gender : Male   Female
Education :
Address :
 

Lets create database connection and class inside functions.php for insert operation and function for insert data into database using mysqli_query.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','demo');

class dbcon
{
public $con;
public function __construct()
{
$this->con = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
}
public function insertData($fname,$email,$contact,$gender,$education,$address)
{
$query = "INSERT INTO users(name,email,contact,gender,education,address) 
values('$fname','$email','$contact','$gender','$education','$address')"; $result=mysqli_query($this->con,$query); return $result; } }
After that we can include the functions.php file inside our users.php and create our class object and called the insert function using class object. All things are done. Now go to your browser and run your users.php file and fill the form and click on submit and check your database table data is inserted. If you have any error contact me or message me.

Comments

Popular posts from this blog

Prestashop Pass Controller Variables To TPL

Prestashop Create Admin Controller inside Admin Folder

Prestashop PDF Download Error