php 如何在PHP中从数据库填充输入类型文件值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19721123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate input type file value from database in PHP?
提问by Hammad
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.
当有人正在编辑表单并且不想更改他之前在编辑时输入的图片时,如何加载输入类型的值。
采纳答案by Nabin Kunwar
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
.
您可以将value
字段设为空并在该输入字段的上方(或下方)显示您的旧图像。然后在提交表单后检查,if
$_POST['userfile']
是否为空不要更新table.picture_name
。
回答by lessoncup
you can give the value attribute to input file type
您可以为输入文件类型提供 value 属性
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;
}
回答by Maaz Khan
The simple trick is that ; give an id to the tag
简单的技巧是:给标签一个 id
<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:
然后在 PHP 中使它像这样:
$oldfile = $_POST['old'];
$file = $_FILES['file']['name'];
if($file!="") {
move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);
} else {
$file = $oldfile;
}