Thursday, 12 May 2016

Update or Edit Data from Mysql Table using OOPS in PHP

This is one more blog on crud database operation, here I will describe how to use php object oriented programming concept for edit or update data of mysql table. If you use PHP OOPs concept for programming then at later time you can easily maintain your code. You to change in one function then it will affect whole code where that was called. In previous post I have already created on class and in that class I have write many different function for different operation. For update of edit data using OOPs concept, I have create one function with name update with three parameter one is table name, second is the array of data in associative array format you want to update and third parameter is for where condition also in associative array. By using loop I will make update query and execute that query. This way this update function will work and We can use this function as many times. This way we can use PHP Object Oriented Programming concept for update or edit mysql data.

Source Code

database.php


 <?php   
 //database.php  
 class Databases{  
      public $con;  
      public function __construct()  
      {  
           $this->con = mysqli_connect("localhost", "root", "", "testing");  
           if(!$this->con)  
           {  
                echo 'Database Connection Error ' . mysqli_connect_error($this->con);  
           }  
      }  
      public function insert($table_name, $data)  
      {  
           $string = "INSERT INTO ".$table_name." (";            
           $string .= implode(",", array_keys($data)) . ') VALUES (';            
           $string .= "'" . implode("','", array_values($data)) . "')";  
           if(mysqli_query($this->con, $string))  
           {  
                return true;  
           }  
           else  
           {  
                echo mysqli_error($this->con);  
           }  
      }  
      public function select($table_name)  
      {  
           $array = array();  
           $query = "SELECT * FROM ".$table_name."";  
           $result = mysqli_query($this->con, $query);  
           while($row = mysqli_fetch_assoc($result))  
           {  
                $array[] = $row;  
           }  
           return $array;  
      }  
      public function select_where($table_name, $where_condition)  
      {  
           $condition = '';  
           $array = array();  
           foreach($where_condition as $key => $value)  
           {  
                $condition .= $key . " = '".$value."' AND ";  
           }  
           $condition = substr($condition, 0, -5);  
           $query = "SELECT * FROM ".$table_name." WHERE " . $condition;  
           $result = mysqli_query($this->con, $query);  
           while($row = mysqli_fetch_array($result))  
           {  
                $array[] = $row;  
           }  
           return $array;  
      }  
      public function update($table_name, $fields, $where_condition)  
      {  
           $query = '';  
           $condition = '';  
           foreach($fields as $key => $value)  
           {  
                $query .= $key . "='".$value."', ";  
           }  
           $query = substr($query, 0, -2);  
           /*This code will convert array to string like this-  
           input - array(  
                'key1'     =>     'value1',  
                'key2'     =>     'value2'  
           )  
           output = key1 = 'value1', key2 = 'value2'*/  
           foreach($where_condition as $key => $value)  
           {  
                $condition .= $key . "='".$value."' AND ";  
           }  
           $condition = substr($condition, 0, -5);  
           /*This code will convert array to string like this-  
           input - array(  
                'id'     =>     '5'  
           )  
           output = id = '5'*/  
           $query = "UPDATE ".$table_name." SET ".$query." WHERE ".$condition."";  
           if(mysqli_query($this->con, $query))  
           {  
                return true;  
           }  
      }  
 }  
 ?>  

test_class.php


 <?php  
 //test_class.php  
 include 'database.php';  
 $data = new Databases;  
 $success_message = '';  
 if(isset($_POST["submit"]))  
 {  
      $insert_data = array(  
           'post_title'     =>     mysqli_real_escape_string($data->con, $_POST['post_title']),  
           'post_desc'          =>     mysqli_real_escape_string($data->con, $_POST['post_desc'])  
      );  
      if($data->insert('tbl_posts', $insert_data))  
      {  
           $success_message = 'Post Inserted';  
      }       
 }  
 if(isset($_POST["edit"]))  
 {  
      $update_data = array(  
           'post_title'     =>     mysqli_real_escape_string($data->con, $_POST['post_title']),  
           'post_desc'          =>     mysqli_real_escape_string($data->con, $_POST['post_desc'])  
      );  
      $where_condition = array(  
           'post_id'     =>     $_POST["post_id"]  
      );  
      if($data->update("tbl_posts", $update_data, $where_condition))  
      {  
           header("location:test_class.php?updated=1");  
      }  
 }  
 if(isset($_GET["updated"]))  
 {  
      $success_message = 'Post Updated';  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | Update or Edit Data from Mysql Table using OOPS in PHP</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
      </head>  
      <body>  
           <br /><br />  
           <div class="container" style="width:700px;">  
                <form method="post">  
                     <?php  
                     if(isset($_GET["edit"]))  
                     {  
                          if(isset($_GET["post_id"]))  
                          {  
                               $where = array(  
                                    'post_id'     =>     $_GET["post_id"]  
                               );  
                               $single_data = $data->select_where("tbl_posts", $where);  
                               foreach($single_data as $post)  
                               {  
                     ?>  
                          <label>Post Title</label>  
                          <input type="text" name="post_title" value="<?php echo $post["post_title"]; ?>" class="form-control" />  
                          <br />  
                          <label>Post Description</label>  
                          <textarea name="post_desc" class="form-control"><?php echo $post["post_desc"]; ?></textarea>  
                          <br />  
                          <input type="hidden" name="post_id" value="<?php echo $post["post_id"]; ?>" />  
                          <input type="submit" name="edit" class="btn btn-info" value="Edit" />  
                     <?php  
                               }  
                          }  
                     }  
                     else  
                     {  
                     ?>  
                          <label>Post Title</label>  
                          <input type="text" name="post_title" class="form-control" />  
                          <br />  
                          <label>Post Description</label>  
                          <textarea name="post_desc" class="form-control"></textarea>  
                          <br />  
                          <input type="submit" name="submit" class="btn btn-info" value="Submit" />  
                     <?php  
                     }  
                     ?>  
                     <span class="text-success">  
                     <?php  
                     if(isset($success_message))  
                     {  
                          echo $success_message;  
                     }  
                     ?>  
                     </span>  
                </form>  
                <br />  
                <div class="table-responsive">  
                     <table class="table table-bordered">  
                          <tr>  
                               <td width="30%">Post Name</td>  
                               <td width="50">Post Description</td>  
                               <td width="10%">Edit</td>  
                               <td width="10%">Delete</td>  
                          </tr>  
                          <?php  
                          $post_data = $data->select('tbl_posts');  
                          foreach($post_data as $post)  
                          {  
                          ?>  
                          <tr>  
                               <td><?php echo $post["post_title"]; ?></td>  
                               <td><?php echo substr($post["post_desc"], 0, 200); ?></td>  
                               <td><a href="test_class.php?edit=1&post_id=<?php echo $post["post_id"]; ?>">Edit</a></td>  
                               <td><a href="#" id="<?php echo $post["post_id"]; ?>" class="delete">Delete</a></td>  
                          </tr>  
                          <?php  
                          }  
                          ?>  
                     </table>  
                </div>  
           </div>  
      </body>  
 </html>  

1 comment: