PHP Deleting Image In MySQL

PHP Deleting Image In MySQL

In this tutorial, we are going to discuss PHP Deleting Image In MySQL. We're gonna use Delete Statement to delete an image from our database table. This will be easy if you read or follow our previous tutorial in PHP Insert Image In MySQL. "tbl_image" Example Database Table. Our Data Result Data

Deleting Image In MySQL Using PDO

Deleting image in our "tbl_image" table.

Form Field - Modal Form

  1. <!-- Modal -->
  2. <div id="delete<?php echo $id;?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  3. <div class="modal-header">
  4. <h3 id="myModalLabel">Delete</h3>
  5. </div>
  6. <div class="modal-body">
  7. <div class="alert alert-danger">
  8. <?php if($row['image_location'] != ""): ?>
  9. <img src="uploads/<?php echo $row['image_location']; ?>" width="100px" height="100px" style="border:1px solid #333333;">
  10. <?php else: ?>
  11. <img src="images/default.png" width="100px" height="100px" style="border:1px solid #333333; margin-left:15px;">
  12. <?php endif; ?>
  13. <b style="color:blue; margin-left:25px; font-size:30px;"><?php echo $row['first_name']." ".$row['last_name']; ?></b>
  14. <br />
  15. <p style="font-size: larger; text-align: center;">Are you Sure you want to Delete?</p>
  16. </div>
  17. <hr>
  18. <div class="modal-footer">
  19. <button class="btn btn-inverse" data-dismiss="modal" aria-hidden="true">No</button>
  20. <a href="delete.php<?php echo '?tbl_image_id='.$id; ?>" class="btn btn-danger">Yes</a>
  21. </div>
  22. </div>
  23. </div>

DELETE Statement

  1. <?php
  2. require_once('db.php');
  3.  
  4. $get_id=$_GET['tbl_image_id'];
  5.  
  6. // sql to delete a record
  7. $sql = "Delete from tbl_image where tbl_image_id = '$get_id'";
  8.  
  9. // use exec() because no results are returned
  10. $conn->exec($sql);
  11. header('location:index.php');
  12. ?>

Output:

Deleting image. Result - ModalAfter deleting the image. Result - Data Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment