5

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.

Hammad
  • 2,097
  • 3
  • 26
  • 45
  • Do you get any errors or any indication that `$discount_details->picture_name` actually contains a value? Right after you pull it from the database, dump the value and see that you get. `var_dump($discount_details->picture_name)` – lampwins Nov 01 '13 at 05:12
  • Display his previous image with file field on html form.and check for empty file field and if not empty replace the file with new one. – Mahmood Rehman Nov 01 '13 at 05:13
  • @Hammad sir i want to ask how did you you echo the value of that input text without using session please answer –  Sep 03 '14 at 07:22

4 Answers4

4

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;

}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
lessoncup
  • 57
  • 3
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.

Nabin Kunwar
  • 1,965
  • 14
  • 29
0

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;
    }
Maaz Khan
  • 67
  • 1
  • 2
-1

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.
}
Jay Gohel
  • 1
  • 2