Home >>PHP Object Oriented >PHP Data Insert using OOP
First We are going to create a class "Database". This class has two main methods, one is connect which contains connectivity Code with PHP and MySQL. another method is saveRecords(). It has three arguments : first argument is database table name, Second argument and third argument is for data(inputted by user using HTML Form).
<?php class Database { var $host="localhost"; var $user="root"; var $pass=""; var $db="resturant"; public function connect() { $con=mysqli_connect($this->host,$this->user,$this->pass,$this->db); return $con; } public function saveRecords($tbName,$u,$p) { $conn=$this->connect(); mysqli_query($conn,"insert into $tbName values('','".$u."','".$p."')"); } } ?>
<?php $obj=new Database(); extract($_POST); //Saved Records Inside Database if(isset($save)) { //here admin is table name, $userName and $pass entered by html form $obj->saveRecords("admin",$userName,$Pass); echo "Records Saved "; } ?>
<form method="post"> <table width="457" border="2" cellspacing="5" cellpadding="5"> <tr> <th width="333" scope="row">Enter your user id </th> <td width="81"><input type="text" name="userName"/></td> </tr> <tr> <th scope="row">enter your password </th> <td><input type="text" name="Pass"/></td> </tr> <tr> <th colspan="2" scope="row"> <input type="submit" value="save" name="save"/> </th> </tr> </table> </form>
Total Downloads : 106
Login / Register To Download