PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 29, 2022

[FIXED] How to keep the previous uploaded image (in db) after submitting a form without selecting a new image to be uploaded

 July 29, 2022     forms, image, php, upload     No comments   

Issue

I have a simple form for creating an article: Title,image,category,body , etc.. My problem is with the image input.

Selecting a new one and submitting everything works fine:

  • the image is being uploaded to the server
  • the title of the image is being saved to db
  • and i can print it in the article.

By editing the whole form, filling all fields but leaving the image field as it is, and finally submitting, the image field value in db is changing to nothing.

How can i configure the php file, so every time the user submits the form without selecting an image (if there was an image pre uploaded in that article ) keep as submitted the previous image (from db) instead of nothing.?


If these informations can be of any help:

I can print the image like this : <?php echo '<img src="'.$results['article']->img.'" width="100px">'; ?>

Simple input field for image:

<input type="file" name="fileToUpload" id="fileToUpload"  value="<?php echo $results['article']->img ?>" />

Solution

What about something like this?

if(!empty($_FILES['fileToUpload']['name'])) //new image uploaded
{
   //process your image and data
   $sql = "UPDATE table SET name=?, image=?,... WHERE id = ?";//save to DB with new image name
}
else // no image uploaded
{
   // save data, but no change the image column in MYSQL, so it will stay the same value
   $sql = "UPDATE table SET name=?,... WHERE id = ?";//save to DB but no change image column
}
//process SQL query in $sql


Answered By - zdeniiik
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing