【发布时间】:2013-10-25 07:25:58
【问题描述】:
当填充更新表单的字段时,如何显示当前图像的路径,如下所示
因此,如果用户不想更改图像,则使用相同的图像。
这是浏览器显示的内容:
<input type="file" value="uploads/chaps_B.jpg" name="image_edit"></input>
我感觉我的photopath 不在范围内,但不确定如何修复它。所有变量都填充表单并保存到数据库中,只是无法确定图像路径。
if(isset($_POST['update_submit']))
{
$get_products = $db->prepare("select `photopath` from `item` where
`item_id` = '$get_item' LIMIT 1");
$get_products->execute();
$path= $get_products->fetchColumn(6);
if($_FILES['image_edit']['tmp_name'] != "")
{
$edit_name = $_POST['item_name'];
$edit_description =$_POST['item_description'];
$form_id = $_POST['edit_form_id'];
$file_dir = "uploads/";
$image = $file_dir. $_FILES['image_edit']['name'];
move_uploaded_file($_FILES['image_edit']['tmp_name'], $image);
$edit_sql =$db->prepare("UPDATE item SET item_name = '$edit_name',
item_description = '$edit_description',
photopath ='$image' WHERE item_id = '$form_id'");
$edit_sql->execute();
header("Location: manage_items.php");
exit();
}
else
{
$edit_name = $_POST['item_name'];
$edit_description =$_POST['item_description'];
$form_id = $_POST['edit_form_id'];
$edit_sql =$db->prepare("UPDATE item SET item_name = '$edit_name',
item_description = '$edit_description'
WHERE item_id = '$form_id'");
$edit_sql->execute();
header("Location: manage_items.php");
exit();
}
更新表格
<form action="item_edit.php" method="post" enctype="multipart/form-data">
<input type = "text" name="item_name" value="<?php echo $item_name ?> "/>
<textarea name="item_description"><?php echo $item_description ?></textarea>
<p> <img src="<?php echo $image; ?>" width="75" height="75" /></p>
<input type="file" name="image_edit" />
<input name="edit_form_id" type="hidden" value="<?php echo $get_item ?>">
<p><input type="submit" name="update_submit" value="Update"/></p>
<p><input type="submit" name="cancel_edit" value="Cancel"/></p>
</form>
数据库结构
【问题讨论】:
-
很可能是相对路径问题。你能分享页面的路径和上传目录相对于web root的路径吗?
-
@Akshay 他们在同一个目录中。上传在 htdocs 文件夹中
-
阻止它重新插入数据库的方法是检查该字段的
$_POST是否为空,这样您就可以跳过将其插入数据库的步骤。 -
ok 你必须做什么,在你的表单中 然后当然是在 edit.php 之前if file upload check $image = $_POST['imgEdit']; 如果用户上传文件 $image 变量自动更新他的值,否则你有相同记录的隐藏最后一个图像名称。
-
这个问题的答案可能会对你有所帮助stackoverflow.com/questions/19089020/how-to-update-type-file/…