0

我是 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
                &nbsp;
                <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}"; 
        }
 }  
4

2 回答 2

0

看,问题出在代码中:

if(empty($_POST['photo'])){
        $query = "UPDATE products SET 
                name = '{$name}', 
                visible = {$visible} 
                WHERE id = {$id}"; 
    }

如果您的表单确实具有enctype="multipart/form-data"属性,您的文件将不会转到 $_POST 数组,而是转到 $_FILES 数组。因此,每次通过表单将新文件发送到更新脚本文件时,都会转到 $_FILES['photo'] 并且 $_POST['photo'] 始终为空。这就是您的脚本只是更新表格的原因。

于 2011-09-01T07:59:40.963 回答
0

您缺少INSERT INTO对新项目的查询。如果您要提交新照片,您的第二个查询将不会执行任何操作。

于 2011-09-01T08:06:19.477 回答