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.
- <div id="delete<?php echo $id;?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
- <div class="modal-header">
- <div class="modal-body">
- <div class="alert alert-danger">
- <?php if($row['image_location'] != ""): ?>
- <img src="uploads/<?php echo $row['image_location']; ?>" width="100px" height="100px" style="border:1px solid #333333; margin-left: 30px;">
- <?php else: ?>
- <img src="images/default.png" width="100px" height="100px" style="border:1px solid #333333; margin-left: 30px;">
- <?php endif; ?>
- <form action="edit_PDO.php<?php echo '?tbl_image_id='.$id; ?>" method="post" enctype="multipart/form-data">
- <div style="color:blue; margin-left:150px; font-size:30px;">
- <input type="file" name="image" style="margin-top:-115px;">
- <div class="modal-footer">
- $conn = new PDO('mysql:host=localhost; dbname=upload_image','root', '');
- require_once ('db.php');
- move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "UPDATE tbl_image SET image_location ='$location1' WHERE tbl_image_id = '$get_id' ";
- echo "<script>alert('Successfully Updated!!!'); window.location='index.php'</script>";
After 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.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.