Skip to content Skip to sidebar Skip to footer

How To Populate Input Type File Value From Database In PHP?

I am writing the code for editing a form that contains an input file field. I am getting all the values pulled from database for different field types but the file type input does

Solution 1:

you can give the value attribute to input file type

if you want to show the file content while updating form you can show it in separate tag

like:

<input type="file" /> <span><?php echo $row[column_name]?></span>

here you should consider one thing

if the use is selected new file to upload you can update the column else the use not selected any thing just updated other content without file you should update the column name with old file name.

$file = $_FILES['file']['name'];


if($file!="") {

move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);

} else {

$file = $oldfile;

}

Solution 2:

You can just make value field empty and show your old image at just up of that input field(or below).then check after submitting form, if $_POST['userfile'] is empty don't update table.picture_name.


Solution 3:

The simple trick is that ; give an id to the tag

    <input type="file" name="file" /> <span name="old" value="<?=$row[column_name]?>"><?php echo $row[column_name]?></span>

Then in PHP make it like this:

    $oldfile = $_POST['old'];
    $file = $_FILES['file']['name'];
    if($file!="") {
      move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);
    } else {
    $file = $oldfile;
    }

Solution 4:

you can write this way

Step 1: Fetch your image in a variable. like this $pimg = $result['pimage'];

Step 2: write html code for add file. <input type="file" name=""image">

Step3: in PHP fetch the file if image upload by user. like this $pimage = $_FILES['images']['name'];

Step 4: now check if the user uploaded the file or not.

if(empty(file name)){
    if yes then update image it.
}else{
   if no then priviously uploaded image use it.
}

Post a Comment for "How To Populate Input Type File Value From Database In PHP?"