Monday, March 27, 2017

How to delete Images of a folder permanently and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo.

In this section, we are going to delete Images of a folder permanently and also data and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo..
This is just a simple techniques to delete Images of a folder permanently and also data and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo... This is soo easy to do it.
To do this first just create database dataadd  and then create table as posts and then create columns  post_id ,post_title,image_title and post_image as integer,varchar,varchar and varchar respectively..

First create uploads folder for images saving on it and displaying on your browser through server.

Now Lets Begin, Create index.php file.

and create form on index.php as like this
  1.  <form method="post" action="upload.php"  enctype="multipart/form-data">
  2. <table class="table1">
  3.     <tr>
  4.         <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
  5.         <td width="30"></td>
  6.         <td><input type="text" name="post_title" placeholder="image_title" required /></td>
  7.     </tr>
  8.     <tr>
  9.         <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
  10.         <td width="30"></td>
  11.         <td><input type="text" name="image_title" placeholder="image_title" required /></td>
  12.     </tr>
  13.     <tr>
  14.         <td><label style="color:#3a87ad; font-size:18px;">Select  Image</label></td>
  15.         <td width="30"></td>
  16.         <td><input type="file" name="image"></td>
  17.     </tr>    
  18.      
  19. </table> 
  20. <button type="submit" name="Submit" class="btn btn-success">Submit</button>   
  21. </form>

Now lets jump on php pdo scripts to proceed for database connection first.

create new php file again and save it as db.php and paste these scripts on this file.
  1. <?php
  2. //this scripts connect to your database
  3.     $db_host = "localhost";
  4.     $db_name = "dataadd";
  5.     $db_user = "root";
  6.     $db_pass = "";
  7.     try{
  8.         $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
  9.         $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     }
  11.     catch(PDOException $e){
  12.         echo $e->getMessage();
  13.     }
  14. ?>

And Again lets create upload.php where our form gonna submited on this page and proceed for uploading and adding texts on uploads folder and mysql table that we have created.

Add some scripts for submiting process of our form now on upload.php
  1. <?php
  2.      include('db.php');
  3.     if(isset($_POST['Submit']))
  4.     {
  5.         
  6.         $post_image=$_FILES["image"]["name"];
  7. $post_title=$_POST['post_title'];
  8. $image_title=$_POST['image_title'];
  9. }
  10.     
  11.         try{
  12.              move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
  13.             //this query is for insertion process
  14.            $stmt = $db_con->prepare("INSERT INTO posts(image_title,post_image,post_title) VALUES( :image_title, :post_image, :post_title)");
  15.             $stmt->bindParam(":post_title", $post_title);
  16.             $stmt->bindParam(":image_title", $image_title);
  17.             $stmt->bindParam(":post_image", $post_image);
  18.             
  19.             
  20.             if($stmt->execute())
  21.             {
  22.                 echo "<script>alert('Successfully Added..')</script>";    
  23.         echo "<script>window.open('index.php','_self')</script>";
  24.             }
  25.             else{
  26.                 echo "Query Problem";
  27.             }    
  28.         }
  29.         catch(PDOException $e){
  30.             echo $e->getMessage();
  31.         }    
  32. ?>

And Now create delete.php file and write some scrpts which delete our mysql table datas and also delete images of our folder uploads permanently.

<?php
require_once('db.php');
$get_id=$_GET['post_id'];
//query for selecting database table 
$stmt = $db_con->prepare("SELECT * FROM  posts WHERE post_id = '$get_id'");
        $stmt->execute();
 while($row=$stmt->fetch(PDO::FETCH_ASSOC))
        {
            extract($row);
            ?>
          <?php  unlink("uploads/".$row['post_image']); ?> // this unlink function delete uploaded images permanently
            <?php
        }
        ?>
         <?php
    $get_id=$_GET['post_id'];
// sql to delete a record
$sql = "Delete from posts where post_id = '$get_id'";
// use exec() because no results are returned
$db_con->exec($sql);
header("Location: index.php");
?>

And last to display uploaded images  from the uploads folder and text or image title post title from the database table posts just copy and paste thes scripts on your index.php file .

  1.  <table class="table table-striped">
  2.     <thead>
  3.       <tr>
  4.         <th>Title</th>
  5.         <th>Image Title</th>
  6.         <th>Images</th>
  7.          <th>Delete</th>
  8.       </tr>
  9.     </thead>  
  10.         <?php
  11.         include('db.php');
  12.         //query for selecting database table
  13.        $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
  14. $results->execute();
  15.         // this while statement displays rows of database table
  16.         while($row=$results->fetch(PDO::FETCH_ASSOC))
  17.         {
  18.             extract($row);
  19.             ?>         
  20.             <tbody>
  21.            <tr>
  22.         <td><?php echo $row['post_title']; ?></td>
  23.          <td><?php echo $row['image_title']; ?></td>
  24.          
  25.          <td><img src="uploads/<?php echo $row['post_image']; ?>" class="img-rounded" alt="image" style="width:60px" height="60px;">
  26.             </td>
  27.            <td><a href="delete.php?post_id=<?php echo $post_id; ?>" class="btn btn-danger">Delete</a> </td>
  28.             </tr>
  29.             </tbody>
  30.             <?php
  31.         }
  32.         ?>        
  33.         </table>

  • full index.php file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title> How to delete Images of a folder permanently.</title>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
  7.  <script src="../js/jquery.min.js"></script>
  8.   <script src="../bootstrap/bootstrap.min.js"></script>  
  9. </head>
  10. <body>
  11.     <div class="container">
  12.   <div class="thumbnail">
  13.   <h4 style="color:#F06"> How to delete Images of a folder permanently and Insert or Add Data and Display in a bootstrap table along with uploading and displaying images with php pdo.</h4>
  14.  <form method="post" action="upload.php"  enctype="multipart/form-data">
  15. <table class="table1">
  16.     <tr>
  17.         <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
  18.         <td width="30"></td>
  19.         <td><input type="text" name="post_title" placeholder="image_title" required /></td>
  20.     </tr>
  21.     <tr>
  22.         <td><label style="color:#3a87ad; font-size:18px;">Image Title</label></td>
  23.         <td width="30"></td>
  24.         <td><input type="text" name="image_title" placeholder="image_title" required /></td>
  25.     </tr>
  26.     <tr>
  27.         <td><label style="color:#3a87ad; font-size:18px;">Select  Image</label></td>
  28.         <td width="30"></td>
  29.         <td><input type="file" name="image"></td>
  30.     </tr>    
  31.      
  32. </table> 
  33. <button type="submit" name="Submit" class="btn btn-success">Submit</button>   
  34. </form>
  35. </div>
  36.          
  37.       <table class="table table-striped">
  38.     <thead>
  39.       <tr>
  40.         <th>Title</th>
  41.         <th>Image Title</th>
  42.         <th>Images</th>
  43.          <th>Delete</th>
  44.       </tr>
  45.     </thead>  
  46.         <?php
  47.         include('db.php');
  48.         //query for selecting database table
  49.        $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
  50. $results->execute();
  51.         // this while statement displays rows of database table
  52.         while($row=$results->fetch(PDO::FETCH_ASSOC))
  53.         {
  54.             extract($row);
  55.             ?>   
  56.           
  57.             <tbody>
  58.            <tr>
  59.         <td><?php echo $row['post_title']; ?></td>
  60.          <td><?php echo $row['image_title']; ?></td>
  61.          
  62.          <td><img src="uploads/<?php echo $row['post_image']; ?>" class="img-rounded" alt="image" style="width:60px" height="60px;">
  63.             </td>
  64.            <td><a href="delete.php?post_id=<?php echo $post_id; ?>" class="btn btn-danger">Delete</a> </td>
  65.             </tr>
  66.             </tbody>
  67.             <?php
  68.         }
  69.         ?>
  70.         
  71.         </table>
  72.        </div>
  73. </body>
  74. </html>

  • full upload.php file 
  1.  <?php
  2.      include('db.php');
  3.     if(isset($_POST['Submit']))
  4.     {
  5.         
  6.         $post_image=$_FILES["image"]["name"];
  7. $post_title=$_POST['post_title'];
  8. $image_title=$_POST['image_title'];
  9. }
  10.     
  11.         try{
  12.              move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
  13.             $stmt = $db_con->prepare("INSERT INTO posts(image_title,post_image,post_title) VALUES( :image_title, :post_image, :post_title)");
  14.             $stmt->bindParam(":post_title", $post_title);
  15.             $stmt->bindParam(":image_title", $image_title);
  16.             $stmt->bindParam(":post_image", $post_image);
  17.             
  18.             
  19.             if($stmt->execute())
  20.             {
  21.                 echo "<script>alert('Successfully Added..')</script>";    
  22.         echo "<script>window.open('index.php','_self')</script>";
  23.             }
  24.             else{
  25.                 echo "Query Problem";
  26.             }    
  27.         }
  28.         catch(PDOException $e){
  29.             echo $e->getMessage();
  30.         }    
  31. ?>
  • Full db.php file
  1. <?php
  2. date_default_timezone_set("Asia/Kathmandu");
  3.     $db_host = "localhost";
  4.     $db_name = "dataadd";
  5.     $db_user = "root";
  6.     $db_pass = "";
  7.     
  8.     try{
  9.         
  10.         $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
  11.         $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.     }
  13.     catch(PDOException $e){
  14.         echo $e->getMessage();
  15.     }
  16. ?>
  • full delete.php file
  1. <?php
  2. require_once('db.php');
  3. $get_id=$_GET['post_id'];
  4. $stmt = $db_con->prepare("SELECT * FROM  posts WHERE post_id = '$get_id'");
  5.         $stmt->execute();
  6.  while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  7.         {
  8.             extract($row);
  9.             ?>
  10.           <?php  unlink("uploads/".$row['post_image']); ?>
  11.             <?php
  12.         }
  13.         ?>
  14.          <?php
  15.     $get_id=$_GET['post_id'];
  16. // sql to delete a record
  17. $sql = "Delete from posts where post_id = '$get_id'";
  18. // use exec() because no results are returned
  19. $db_con->exec($sql);
  20. header("Location: index.php");
  21. ?>

No comments:

Post a Comment