繁体   English   中英

使用php从目录上传图像

[英]uploading images from a dir using php

我正在尝试将图像从目录上传到网页并将其保存到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);
        }
    }
?>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM