使用php

时间:2019-01-20 10:04:39

标签: php

我正在尝试将图像从 dir 上传到网页,并将其保存到 sql数据库,但是我一直收到以下错误消息。

"Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1364 Field 'user' doesn't have a default value in /goinfre/jdubula/Documents/MAMP/apache2/htdocs/jamic/capture_upload.php:28 Stack trace: #0 /goinfre/jdubula/Documents/MAMP/apache2/htdocs/jamic/capture_upload.php(28): PDO->exec('INSERT INTO ima...') #1 {main} thrown in /goinfre/jdubula/Documents/MAMP/apache2/htdocs/jamic/capture_upload.php on line 28"

<?php
    require_once("database.php");
    session_start();
    //This page is only accessible if you're logged in
    if (!isset($_SESSION['id'])){
        header("location:/registration/login.php");
    }
    //When the upload button is clicked the image is uploaded into the uploads file. Only jpg, png and gif extensions are accepted.
    if (isset($_POST['upload'])){
        $file = $_FILES['image'];
        $extensions = array('jpg', 'jpeg', 'gif', 'png');
        $file_text = explode('.', $_FILES['image']['name']);
        $file_ext = strtolower(end($file_text));
        //if the file does not have the specified extensions "Format not accepted"
        if (!in_array($file_ext, $extensions)){
            $alert = "<h5>Format not accepted: Please upload<br>jpg, jpeg, png or gif</h5>";
        }
        //if an error has occured
        elseif($_FILES['image']['error']){
            $alert = "An error occured";
        }
        //create a random name for the image to prevent image overwriting. Upload image to folder and insert image name into the database.
        else {
            $fileNameNew = uniqid('',true).".".$file_ext;
            move_uploaded_file($_FILES['image']['tmp_name'], "uploads/".$fileNameNew);
            $alert = "<h5>File Uploaded successfully</h5>";
            $sql = "INSERT INTO image (img,article_likes) VALUES ('\"uploads/\".$fileNameNew', 0)";
            $connection->exec($sql);
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

您的image表中还有另一个不可为空的列user->您必须在此列中填充有效值才能使其成功