mysqli_query在php脚本中的文件上传期间返回false

时间:2017-12-10 23:01:42

标签: php mysql

我是PHP的新手,我正在尝试将文本文件上传到我的数据库。我将上传的文件添加到指定文件夹的部分可以正常工作,但我无法向数据库添加任何内容。我发现它的原因是my_sqli_query返回false,但我不知道,因为它是什么原因。感谢帮助!

<?php

    $dbServername = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbName = "project";

    $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

    if(isset($_POST['submit'])) {
        $file = $_FILES['file'];

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

        $book_title = mysqli_real_escape_string($conn, $_POST['book_title']);
        $book_author = mysqli_real_escape_string($conn, $_POST['book_author']);
        $book_publisher = mysqli_real_escape_string($conn, $_POST['book_publisher']);
        $book_year = mysqli_real_escape_string($conn, $_POST['book_year']);
        $book_genre = mysqli_real_escape_string($conn, $_POST['book_genre']);
        $fileExt = explode('.', $fileName);
        $fileAExt = strtolower(end($fileExt));
        $allowedExtensions = array('epub', 'mobi', 'pdf', 'txt');

        if (empty($book_title) || empty($book_author) || empty($book_publisher) || empty($book_year) || empty($book_genre)) {
            header("Location: ../upload.php?empty");
            exit();
        }else{
            if (in_array($fileAExt, $allowedExtensions)) {
                if ($fileError === 0) {
                    if ($fileSize < 100000) {
                        $fileNewFilename = uniqid('', true) . $fileName . "." . $fileAExt;
                        $fileDest = 'uploads/' . $fileNewFilename;
                        $sql = "INSERT INTO book_db (book_title, book_author, book_publisher, book_year, book_year, file) VALUES ('$book_title', '$book_author', '$book_publisher', '$book_year', '$book_genre', '$file');";
                        $query = mysqli_query($conn, $sql);
                        var_dump($query);
                        move_uploaded_file($fileTmpName, $fileDest);
                        if ($query != null) {
                            header("Location: index.php?fail");
                        } else {
                            header("Location: index.php?fail");
                        }
                    } else {
                        echo 'fail, too large';
                    }
                } else {
                    echo 'fail, error';
                }
            } else {
                echo 'fail';
            }
        }
    }

0 个答案:

没有答案