PHP无法将图像文件写入SQL数据库

时间:2019-03-25 22:04:24

标签: php

当我尝试将数据插入到mysql表中时,在图像部分'Notice:Undefined index'中收到错误,并且该表的该列中未写入任何内容。表列的格式设置为longblob。我该如何解决?


<?php

    echo '
    <!-- ==================================== -->


        <p>Preview & Item description:</p>
        <div class="container1">
            <br><br>





<form action="" method="post"  name="save_item" enctype="multipart/form-data">

            <input class="uploadpics" name="file" type="file" onchange="preview_image(event)">



            <div class="itemName">
                <textarea rows="1" cols="1" maxlength="30" name="item_name" id="item_name"></textarea>
            </div>';

            echo '<div class="itemID" id="itemID">'.mktime();
                echo '</div>';

            echo'
            <div class="prevpic"><img id="output_image" /></div>

            <div class="itemdesc">
                <textarea rows="1" cols="1" maxlength="650" name="item_description"
                    id="item_description"></textarea>
            </div>



            <div class="price">
                <textarea rows="1" cols="1" maxlength="14" name="item_price" id="item_price"></textarea>
            </div>
        </div>

        <!-- dropdown category -->
        <select id="cat_selector1" name="cat_selector1">
                <option value="c1">Category 1</option>
                <option value="c2">Category 2</option>
                <option value="c3">Category 3</option>
                <option value="c4">Category 4</option>
                <option value="c5">Category 5</option>
                <option value="c6">Category 6</option>
        </select>

        <button id="save_item" type="submit" name="save_item">Save Product</button>
    </form>

</div>
<!-- ==================================== -->

<!-- script for image preview -->

<script type="text/javascript">
    function preview_image(event) {
        var reader = new FileReader();
        reader.onload = function () {
            var output = document.getElementById("output_image");
            output.src = reader.result;
        }
        reader.readAsDataURL(event.target.files[0]);
    }
</script>

<!-- ==================================== -->

    ';
}

    ?>

第一部分创建html表单,下面的部分是从其中收集数据的代码。

<?php

if(isset($_POST['save_item'])){


    $file = $_FILES['file'];
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'pfd');

    if(in_array($fileActualExt, $allowed)){
        if($fileError===0){
            if($fileSize < 1000000){
                $fileNameNew = uniqid('', true). ".".$fileActualExt;
                $fileDestination = 'uploads/' .$fileNameNew;
                move_uploaded_file($fileTmpName,$fileDestination);


                $item_ID = mktime();
                $item_name = $_POST['item_name'];
                $item_description = $_POST['item_description'];
                $item_price = $_POST['item_price'];
                $item_pic = $_POST['$file'];
                $item_cat1 = $_POST['cat_selector1'];
                $item_cat2 = "empty";





            }else{
                echo "Your file is too big!";
            }
        }else{
            echo "There was an error uploading your file";
        }

    }else{
        echo "You cannot upload files of this type!";
    }

    $sql = "INSERT INTO `products_db` (`p_id`, `p_name`, `p_desc`, `p_price`,`p_pic`, `p_cat1`, `p_cat2`) VALUES ('$item_ID', '$item_name', '$item_description', '$item_price', '$item_pic', '$item_cat1', '$item_cat2');";
    mysqli_query($conn, $sql);

}



?>

0 个答案:

没有答案