PHP Update Image In MySQL

Submitted by: 
Visitors have accessed this post 26430 times.

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.


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


Modal Form

ResultAfter Update The Current Image

As you can see the current image was updated.


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.


Hi sir. Do you have some tutorial about uploading avatar? tnx

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.