How to Insert Data in Database using PHP OOPS Concept
In this post I will explain how to insert data in database using oops concept.
1.First create a html form
Index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<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" maxlength="10" value="" class="form-control" required /></td> </tr> <tr> <th height="60" scope="row">Gender :</th> <td><input type="radio" name="gender" value="Male" required /> Male <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">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="60" scope="row">Address :</th> <td><textarea name="address" class="form-control"> </textarea></td> </tr> <tr> <th height="60" scope="row"> </th> <td><input type="submit" value="Submit" name="submit" class="btn-primary" /></td> </tr> </table> </form> |
2. Create a database with name demos
Inside demos data base create a table inserdata
Sample Structure of insertdata table
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE IF NOT EXISTS `insertdata` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `contactno` bigint(20) NOT NULL, `gender` varchar(255) NOT NULL, `education` varchar(255) NOT NULL, `addrss` longtext NOT NULL, `posting_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; |
3. Create a php page function .php
Inside this page a class DB_con define. Db_con class contain db connection and insert funtion.
function .php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php define('DB_SERVER','localhost'); define('DB_USER','root'); define('DB_PASS' ,''); define('DB_NAME', 'demos'); 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 insertdata(name,email,contactno,gender,education,addrss)<br>values('$fname','$email','$contact','$gender','$education','$adrss')"); return $ret; } } ?> |
4.Define a object and class the insert function
Put this code at the top of the index.php page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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>"; } } ?> |