Skip to main content

PHP Update Image In MySQL

Screenshot
Body

PHP Update Image In MySQL

We have already a tutorial for PHP Insert Image In MySQL and PHP Deleting Image In MySQL. So, I decided to create a follow-uo tutorial for PHP Update Image In MySQL. In this tutorial, we are going to update the current Image to a new one. For this tutorial, we are going to use the UPDATE Statement to update the current image in our table. Let's start with: This is the image that we are going to edit. Result

Updating Image

Modal Form Field

  1. <div id="delete<?php echo $id;?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  2. <div class="modal-header">
  3. <h3 id="myModalLabel">Update</h3>
  4. </div>
  5. <div class="modal-body">
  6. <div class="alert alert-danger">
  7. <?php if($row['image_location'] != ""): ?>
  8. <img src="uploads/<?php echo $row['image_location']; ?>" width="100px" height="100px" style="border:1px solid #333333; margin-left: 30px;">
  9. <?php else: ?>
  10. <img src="images/default.png" width="100px" height="100px" style="border:1px solid #333333; margin-left: 30px;">
  11. <?php endif; ?>
  12. <form action="edit_PDO.php<?php echo '?tbl_image_id='.$id; ?>" method="post" enctype="multipart/form-data">
  13. <div style="color:blue; margin-left:150px; font-size:30px;">
  14. <input type="file" name="image" style="margin-top:-115px;">
  15. </div>
  16. </div>
  17. <hr>
  18. <div class="modal-footer">
  19. <button class="btn btn-inverse" data-dismiss="modal" aria-hidden="true">No</button>
  20. <button type="submit" name="submit" class="btn btn-danger">Yes</button>
  21. </form>
  22. </div>
  23. </div>
  24. </div>

Creating Our Database

  1. <?php
  2. $conn = new PDO('mysql:host=localhost; dbname=upload_image','root', '');
  3. ?>

Creating Our UPDATE Statement

  1. <?php
  2.  
  3. require_once ('db.php');
  4.  
  5. $get_id=$_REQUEST['tbl_image_id'];
  6.  
  7. move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
  8. $location1=$_FILES["image"]["name"];
  9.  
  10.  
  11. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12. $sql = "UPDATE tbl_image SET image_location ='$location1' WHERE tbl_image_id = '$get_id' ";
  13.  
  14. $conn->exec($sql);
  15. echo "<script>alert('Successfully Updated!!!'); window.location='index.php'</script>";
  16. ?>

Output:

Modal Form ResultAfter Update The Current Image As you can see the current image was updated. Result 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.

Comments

Add new comment