开发者

PHP-Mysql Form Problem for Photo Uploading

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" />
   开发者_StackOverflow中文版         </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>

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜