我是 php 新手,正在尝试设计一个系统,我必须在其中上传照片、从中删除照片并编辑照片。我用 move_uploaded_file(); 上传照片。和取消链接();删除照片。我成功地按表格上传和删除照片。但是找不到我在编辑哪里出错了。我的问题是,在编辑表单中时,我没有提供任何新照片进行编辑,mysql 表正在更新。但是当给出新照片时,表单不起作用.. .这是我所做的..在发送部分:
<?php
$product=get_product_by_id($_GET['pid']);/*is a function to get product from database*/
?>
<form enctype="multipart/form-data" action="edit_product.php?pid=<?php echo urlencode($_GET['pid']); ?>" method="post">
<p>Product name:
<input type="text" name="name" value="<?php echo $product['name']?>" id="name" />
</p>
<p>Actual Photo:
<input type="file" name="photo" >
</p>
<p>Thumbnail Photo:
<input type="file" name="thumb" >
</p>
<p>Visible:
<input type="radio" name="visible" value="0" /> No
<input type="radio" name="visible" value="1" /> Yes
</p>
<input type="submit" name="submit" value="Edit Product" />
</form>
接收部分:
if (isset($_POST['submit'])) {
$id = mysql_prep($_GET['pid']);
$name = mysql_prep($_POST['name']);
$visible = mysql_prep($_POST['visible']);
if(empty($_POST['photo'])){
$query = "UPDATE products SET
name = '{$name}',
visible = {$visible}
WHERE id = {$id}";
}
else{
$product=get_product_by_id($id);
//echo $product['photo'];
$target = "images/products/";
$target=$target . $product['photo'];
$target2 = "images/product_thumbs/";
$target2=$target2 . $product['thumb'];
unlink($target);
unlink($target2);
$photo=$_POST['name'].".jpg";
$photo = mysql_prep($photo);
$thumb=$_POST['name']."_thumb.jpg";
$thumb = mysql_prep($thumb);
$target = "images/products/";
$target = $target .$name.".jpg";
$target2 = "images/product_thumbs/";
$target2 = $target2 .$name."_thumb.jpg";
move_uploaded_file($_FILES['photo']['tmp_name'], $target);
move_uploaded_file($_FILES['thumb']['tmp_name'], $target2);
$query = "UPDATE products SET
name = '{$name}',
photo = '{$photo}',
thumb = '{$thumb}',
visible = {$visible}
WHERE id = {$id}";
}
}