PHP-Mysql照片上传的表单问题

发布于 2024-12-02 19:24:12 字数 2786 浏览 1 评论 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}"; 
        }
 }  

I am new in php and trying to design a system, where i have to upload photo, delete photo from it, and edit photos . i used move_uploaded_file(); to upload photos. and unlink(); to delete photo.I did uploading and deleting photo by form successfully. But Cant find where did i go wrong with the editing.my problem is, when in the edit form,i am not giving any new photos to edit ,mysql table is updating.but when new photos are given, the form doesnt work ... here is what i did..
in the sending part:

<?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>

recieving part:

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}"; 
        }
 }  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手长情犹 2024-12-09 19:24:12

看,问题出在代码中:

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'] 始终为空。这就是您的脚本仅更新表的原因。

Look, problem is in the code:

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

If your form does have enctype="multipart/form-data" attribute, your file will go NOT TO $_POST array, but to $_FILES array. So every time you send new file through form to your Update script file goes to $_FILES['photo'] and $_POST['photo'] is always empty. That's why your script just updates the table.

黯淡〆 2024-12-09 19:24:12

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

You are missing an INSERT INTO query for new items. Your second query will not do anything if you are submitting a new photo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文