无法在数据库中插入多个图像

时间:2018-08-14 11:21:14

标签: javascript php jquery

代码:

<script>
    $(document).ready(function(){
        $("#add_small").click(function(event){
            event.preventDefault();
            $(".add_small").append('<div class="form-group">\
                                        <label for="product_small_image">Product Image:</label>\
                                        <input type="file" name="product_image[]" class="product_image" value=""/>\
                                        <a href="javascript:void(0)" class="remove_small">Remove</a>\
                                    <div>');
        });

        jQuery(document).on('click', '.remove_small', function() {
            jQuery(this).parent().remove();
            return false;
        });
    });
</script>

<form method="post" name="user_registration" class="register" enctype="multipart/form-data">
    <div class="form-group">
        <label for="product_small_image">Product Image:</label>
        <input type="file" name="product_image[]" class="product_image" value=""/>
        <a href="javascript:void(0)" id="add_small">Add</a>
    </div>
    <div class="add_small"></div>
    <br/>

    <input name="submit" type="submit" class="submit" value="Submit" />
</form>

<?php
    if(isset($_POST['submit']))
    {
        $slide = implode(",",$_FILES['product_image']['name']);
        $slide_img = explode(",",$slide);

        foreach($slide_img as $product_img)
        {       
            $folder_Path = "../images/product/";
            $banner_image_name = str_replace(" ", "", strtolower(basename($product_img)));
            $banner_image_name_upload = $folder_Path.$banner_image_name;
            $banner_image_tmp = $_FILES['product_image']['tmp_name'];
            $imageFileType = strtolower(pathinfo($banner_image_name,PATHINFO_EXTENSION));

            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) 
            {
                $msg = "<div class='alert alert-success'>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</div>";
            }
            else
            {
                if (move_uploaded_file($banner_image_tmp, $banner_image_name_upload)) 
                {
                    $set_width = 600;
                    $set_height = 600;
                    $banner_image_source_file = $banner_image_name_upload;
                    $banner_image_save_file = $banner_image_name_upload;
                    list($width_orig, $height_orig) = getimagesize($banner_image_source_file);
                    $image_p = imagecreatetruecolor($set_width, $set_height);
                    $image = imagecreatefromjpeg($banner_image_source_file);
                    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $set_width, $set_height, $width_orig, $height_orig);
                    imagejpeg($image_p, $banner_image_save_file, 75);

                    $query = "insert into add_in_stock(`product_image`)values('".$product_img."')";

                    $result = mysqli_query($con,$query);
                    if($result==true)
                    {
                        $msg = "<div class='alert alert-success'>Record Save Successfully</div>";
                    }
                    else
                    {
                        $msg = "<div class='alert alert-danger'>Unable to Save Please Try Again !!!</div>";
                    }
                }
                else
                {
                    $msg = "<div class='alert alert-danger'>Unable to Proceeed Please Try Again !!!</div>";
                }       
            }
        }
    }
?>

在这段代码中,我使用jQuery创建了添加和删除更多文件的步骤。现在,当我单击添加按钮时会发生什么,它表明我一次又一次地选择另一个文件。我可以像这样上传多个文件。但是问题是,当我单击“提交”按钮插入数据库并将图像移动到文件夹时,它向我显示错误,即

  

无法继续,请重试

那么,如何解决此问题?

0 个答案:

没有答案