Reputation: 57
I am trying to upload an image from a dir to a web page saving it to a sql database, but i keep getting the following error.
"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);
}
}
?>
Upvotes: 1
Views: 27
Reputation: 1380
Your image
table has another not nullable column user
-> you must fill this column with a valid value for it to succeed
Upvotes: 1