Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate input type file value from database in PHP?

Tags:

html

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 not show its value.

I have a code that looks like this:

<input class="picturebox" id="logo" name="userfile"  value="<?php echo $discount_details->picture_name;?>" />

But actually in rendered view value attribute is null for userfile field.

How do I load the value of input type when someone is editing the form and does not want to alter the picture entered earlier by him upon edit.

like image 686
Hammad Avatar asked Mar 22 '23 19:03

Hammad


2 Answers

you can't 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;

}
like image 179
lessoncup Avatar answered Mar 24 '23 09:03

lessoncup


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.

like image 44
Nabin Kunwar Avatar answered Mar 24 '23 10:03

Nabin Kunwar