Learn how to update image in php without changing the uploaded image fields in different ways.
Usually, when you change the content of a page, which has an input field like an image, it will not get updated dynamically as per the change.
Ecommerce Solutions for Starting and Growing Your Business!!!
Whenever you make changes to the text fields without altering the image update in php, a Temp name will be generated automatically, and it will get stored in the database on each update. As a result of this temp name, the image will be changed with respect to that.
In this blog, we are going to find how to update image in php and the other fields without changing the uploaded image. There are so many ways to perform that, but I am sharing one of the methods here.
How to update image in PHP without changing the uploaded image fields?
Step 1 On How To Update Image In PHP:
Create a new field to pass the stored image from the database.
Note: You need to write your relevant query to retrieve the stored image from the database and call it in the “input value” as I have shared below
<input type="text" name="image_name" value="<?=$rows['IMAGE_NAME'];?>" />
Step 2 On Update Image In PHP:
Paste the below-given code in the form action file.
if(!empty($_FILES['files']['tmp_name']) && ($_POST['image_name']) || !empty($_FILES['files']['tmp_name']) && (empty($_POST['image_name']))){
$errors= array();
$file_name = $key.$_FILES['files']['name'];
$temp = explode(".", $_FILES['files']['name']);
$newfilename = round(microtime(true)) .'.'. end($temp);
$file_size =$_FILES['files']['size'];
$file_tmp =$_FILES['files']['tmp_name'];
$file_type=$_FILES['files']['type'];
if($file_size> 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="Place your Insert Query";
$desired_dir="Path of the image to be stored";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$newfilename)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$newfilename);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$newfilename.time();
rename($file_tmp,$new_dir) ;
}
$mysqli->query($query);
$mid = $mysqli->insert_id;
if($mid > 0): ?>
<script type="text/javascript">
" Redirection path"
</script>
<? endif;
}else{
print_r($errors);
}
if(empty($error)){
echo "Success";
}
}
else if(empty(($_FILES['files']['tmp_name'])) && ($_POST['image_name'])){
$ba_image = $_POST['image_name'];
$mysqli->query("Place your Update Query ");
}
Step 3 On Image Update In PHP:
Now, submit the form. The form takes the input value and the image file upload value (if changed) to the form action file and will check the condition that we have mentioned in the above step.
There are three conditions possible and the action will be performed according to that condition.
Condition 1- If only file upload value is present
If the input value from the image name field is empty and the file upload value is true, it will update the database with the new image name.
Condition 2- If the only input value is present
If the input value from the image name field is true and the file upload value is empty, it will update the database with the old or existing image name.
Condition 3- If both input value and file upload value is present
If the input value from the image name field is true and the file upload value is also true, it will update the database with the new image name from the file upload value.
Ecommerce Solutions for Starting and Growing Your Business!!!
(Note: I have written this value only for single image update in php)
So, here I am giving a sample coding which you can use for reference.
PHP Coding
<?
/* Include your DB connection */
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'database_name';
global $mysqli;
//Connect
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()) {
printf("MySQLi connection failed: ", mysqli_connect_error());
exit();
}
// Change character set to utf8
if (!$mysqli->set_charset('utf8')) {
printf('Error loading character set utf8: %s\n', $mysqli->error);
}
/* Update OR Add Image in DB */
if(isset($_POST['smt_image'])):
if(!empty($_FILES['files']['tmp_name']) && ($_POST['image_name']) || !empty($_FILES['files']['tmp_name']) && (empty($_POST['image_name']))){
$errors= array();
//foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'];
$temp = explode(".", $_FILES['files']['name']);
$newfilename = round(microtime(true)) .'.'. end($temp);
$file_size =$_FILES['files']['size'];
$file_tmp =$_FILES['files']['tmp_name'];
$file_type=$_FILES['files']['type'];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT INTO images (IMAGE_NAME) VALUES('".$newfilename."')";
$desired_dir="assets/images/";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$newfilename)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$newfilename);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$newfilename.time();
rename($file_tmp,$new_dir) ;
}
$mysqli->query("DELETE FROM images WHERE IMAGE_NAME ='".$_POST['image_name']."'");
$mysqli->query($query);
$mid = $mysqli->insert_id;
if($mid > 0): ?>
<script type="text/javascript">
window.location.href="index.php";
</script>
<? endif;
}else{
print_r($errors);
}
//}
if(empty($error)){
echo "Success";
}
}
else if(empty(($_FILES['files']['tmp_name'])) && ($_POST['image_name'])){
$image = $_POST['image_name'];
$mysqli->query("UPDATE images SET IMAGE_NAME ='".$image."' WHERE TITLE = ".$title);
?>
<script type="text/javascript">
window.location.href="index.php";
</script>
<? }
endif; ?>
HTML Coding
<!-- HTML Code -->
<form method="post" action="" enctype="multipart/form-data">
<div class="row nomargin">
<div class="col-md-6 paddingright20">
<div class="form-group">
<label class="control-label col-sm-5">Image Upload <span class="text-danger">*</span></label>
<div class="row col-md-9">
<input type="file" name="files" />
<? if(isset($_GET['id'])):
$sqls = $mysqli->query("SELECT * FROM images WHERE ID ='".$_GET['id']."'");
$rows = mysqli_fetch_assoc($sqls);
?>
<img width="200" src="assets/images/<?=$rows['IMAGE_NAME'];?>" />
<input type="hidden" name="image_name" value="<?=$rows['IMAGE_NAME'];?>" />
<? endif; ?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-sm-offset-3 ">
<button type="submit" name="smt_image" class="btn btn-success">Submit</button>
<a href="index.php?id=<?=$rows['ID'];?>" class="btn btn-success">edit</a>
</div>
</div>
</form>
Now you might have got some basic idea about how the changes can be made. As I have already said, this code written here is only for how to update image in php single upload, and we will tell you about how to perform multi-image upload in our next blog.
If you have any queries regarding PHP Development, contact sales anytime or leave a comment.
Frequently Asked Questions (FAQs) On Update Image In PHP
1. How to update image in PHP without changing the uploaded image?
Update image in PHP without altering the original uploaded image is achievable using various techniques like file manipulation functions and database operations. Check out our detailed guide on how to perform this task efficiently.
2. What are the steps involved in update image in PHP?
Update image in PHP typically involves retrieving the uploaded image, processing the new image data (if any), and then updating the image file or database record accordingly. Our blog provides a step-by-step tutorial to guide you through this process seamlessly.
3. Can I update image in PHP without affecting other uploaded image fields?
Yes, it’s possible to update image in PHP without altering other uploaded image fields. By carefully managing form submissions and processing only the relevant image data, you can ensure that only the intended image is updated while preserving the integrity of other uploaded image fields.
4. How do I update form in PHP to include image updating functionality?
Update form in PHP to incorporate image updating functionality involves adding input fields for both the new image file and any additional data associated with the image. By handling form submissions effectively and implementing the necessary PHP logic, you can seamlessly integrate image updating capabilities into your web application. Check out our blog for detailed insights and code examples.