Update or Edit Data from Mysql Table using PHP OOP
![]() |
Update or Edit Data from Mysql Table using PHP OOP |
Hi Guys in this Lesson we are going to learn How to update or edit data from mysql table in PHP OOP Concepts .
Just follow the below example to learn Update or Edit Data from Mysql Table using PHP OOP
=>Database and Table:
Database Name: test
Table name : user
SQL Codes for Creating user table and inserting rows
=>Database and Table:
Database Name: test
Table name : user
SQL Codes for Creating user table and inserting rows
CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(150) NOT NULL, `email` varchar(150) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `user` (`id`, `username`, `email`) VALUES (NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'), (NULL, 'ANJAN', 'biswasshiuli608@gmail.com'), (NULL, 'Supriya Gain', 'SupriyaStar@Yahoo.com'), (NULL, 'ANJAN KUMAR', 'Anjankumardhali@gmail.com'), (NULL, 'ANJAN', 'biswasshiuli608@gmail.com'), (NULL, 'ANJANBD', 'biswasshiuli608@gmail.com');
Create 3 files named index.php , database.php and update.php
1. index.php
<?php include "database.php"; ?> <?php if(isset($_GET['msg'])){ echo $_GET['msg']; } ?> <?php $db= new Database(); $query="SELECT*FROM user"; $result=$db->select($query); ?> <table width="500px" cellspacing="0" border="1px"> <tr> <th>ID</th> <th>UserName</th> <th>Email</th> <th>Action</th> </tr> <?php if($result) { ?> <?php while($row=$result->fetch_assoc()){?> <tr> <td><?php echo $row['id'];?></td> <td><?php echo $row['username'];?></td> <td><?php echo $row['email'];?></td> <td><a href="update.php?id=<?php echo $row['id'];?>">Update</a></td> </tr> <?php }}?> </table>
2. database.php
<?php class Database{ public $host='localhost'; public $user='root'; public $pass=''; public $dbname='codenair.com'; public $link; public $error; public function __construct(){ $this->connectDB(); } //Connecting Database private function connectDB(){ $this->link = new mysqli($this->host,$this->user,$this->pass,$this->dbname); if(!$this->link){ $this->error= "Connection Failed".$this->link->connect_error; return false; } } //Fetching Data public function select($query){ $result= $this->link->query($query) or die ($this->link->error.__LINE__); if($result->num_rows>0){ return $result; }else{ return false; } } //Updating Data public function update($query){ $update=$this->link->query($query) or die($this->link->error.__LINE__); if($update){ header('location:index.php?msg='.urlencode('Data Updated Successfull.')); } } }
3. update.php
<?php include "config.php"; include "database.php"; ?> <?php $id=$_GET['id']; $db= new Database(); $query="SELECT*FROM user WHERE id='$id'"; $result=$db->select($query); //Update Records if(isset($_POST['submit'])){ $username=$_POST['username']; $email=$_POST['email']; if($username==''&&$email==''){ echo "UserName and Email Must Not be Empty"; }else{ $query="UPDATE user SET username='$username',email='$email' WHERE id='$id'"; $update=$db->update($query); } } ?> <?php if($result) { $row=$result->fetch_assoc();?> <form method="POST"> <table> <tr> <td>UserName:</td> <td><input type="text" name="username" value="<?php echo $row['username'];?>"/></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" value="<?php echo $row['email'];?>"/></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Update"/></td> </tr> </table> </form> <?php }?>
Practice This codes to learn Update or Edit Data from Mysql Table using PHP OOP..Keep Visiting for More Codes and Don't Forget to Leave Your Comment..Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.