-1

I made a function in php that uploads a video from a form. But when I press the submit button I got this error: Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1364 Field 'courses_id' doesn't have a default value in C:\Users\daan\schoolcode\wfflixgroup7\controllers\courses\uploadCourse.php:14 Stack trace: #0 C:\Users\daan\schoolcode\wfflixgroup7\controllers\courses\uploadCourse.php(14): PDOStatement->execute() #1 C:\Users\daan\schoolcode\wfflixgroup7\index.php(12): require('C:\Users\daan\s...') #2 {main} thrown in C:\Users\daan\schoolcode\wfflixgroup7\controllers\courses\uploadCourse.php on line 14.

Everything works perfect the video uploads and everything displays in the database(mariadb). But still got this error that the courses_id has no default value even though its primary key and auto increment. I tried to give it a default value but then the error still appears. Does someone know how to solve this?

My video upload function:


$config = require 'config.php';

$courses_name = $_POST['courses_name'];
$courses_description = $_POST['courses_description'];
$courses_category = $_POST['courses_category'];



$query = Connection::make($config['database'])->prepare("INSERT INTO courses(courses_name, courses_description, courses_category) VALUES(?, ?, ?)");
$query->bindParam(1, $courses_name, PDO::PARAM_STR);
$query->bindParam(2, $courses_description, PDO::PARAM_STR);
$query->bindParam(3, $courses_category, PDO::PARAM_STR);
$query->execute();


//File Upload
if (isset($_POST['submit'])){
    $maxsize= 104857600; //100MB in Bytes
}

if (isset($_FILES['file']['name']) && $_FILES['file']['name'] !=''){
    $name = $_FILES['file']['name'];
    $target_dir = "videos/";
    $target_file = $target_dir.$name;

// file extension
    $extension = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

//  valid file extensions
    $extension_arr = array("mp4", "avi", "3gp", "mov", "mpeg");

//   check extension
    if (in_array($extension, $extension_arr)){

//   check file size
        if ($_FILES['file']['size'] >=$maxsize){
            $_SESSION['message'] = "File too large. file must be less than 100MB";
        } else{
//   upload
            if (move_uploaded_file($_FILES['file']['tmp_name'],$target_file)){
// name kan fout zijn!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                $stmt = Connection::make($config['database'])->prepare("INSERT INTO videos(video_name,video_location) VALUES('".$name."','".$target_file."')");
                $stmt->execute();

                $_SESSION['message'] = "Upload successfully";
            }
        }
    } else{
        $_SESSION['message'] = "Invalid file extension.";
    }


}else{
    $_SESSION['message'] = "Please select a file.";
}
if (!isset($_POST['courses_category'])){
    echo "geen categorie geselecteerd";
}



My form:

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cursus Aanmaken</title>
    <?php require 'views/utils/headerLinks.php';
    require 'utils/videoUpload.php';
    ?>

</head>
<body>
<!--Upload response-->
<?php
if (isset($_SESSION['message'])){
    echo $_SESSION['message'];
    unset($_SESSION['message']);
}

?>
<header>
    <?php require 'utils/navbar.php'; ?>
</header>
<main>

    <!--Makes a container and centers the form-->
    <div class="container">

        <!--Makes a cardholder-->
        <div class="card">
            <div class="card-body">
                <form action="uploadCourse" method="post" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label class="form-check-label" for="courses_name">Naam Cursus</label>
                        <input name="courses_name" class="form-control" type="text"
                               placeholder="Typ hier de naam van de cursus"
                               aria-label="default input example" id="courseName">

                    </div>

                    <!--Description text area-->
                    <div class="mb-3">
                        <label for="exampleFormControlTextarea1" class="form-label">Omschrijving</label>
                        <textarea name="courses_description" class="form-control" id="exampleFormControlTextarea1"
                                  rows="3"
                                  placeholder="Typ hier de beschrijving van de cursus"></textarea>
                    </div>

                    <!--Category checkbox area-->
                    <div><label class="categoryCheckboxes">Categorie(ën)</label></div>
                    <div class="btn-group" role="group" aria-label="Basic checkbox toggle button group">

                        <input name="courses_category" type="checkbox" class="btn-check" id="btncheck1" autocomplete="off" value="html">
                        <label class="btn btn-outline-primary" for="btncheck1">Html</label>

                        <input name="courses_category" type="checkbox" class="btn-check" id="btncheck2" autocomplete="off" value="php">
                        <label class="btn btn-outline-primary" for="btncheck2">Php</label>

                        <input name="courses_category" type="checkbox" class="btn-check" id="btncheck3" autocomplete="off" value="css">
                        <label class="btn btn-outline-primary" for="btncheck3">CSS</label>

                        <input name="courses_category" type="checkbox" class="btn-check" id="btncheck4" autocomplete="off" value="java">
                        <label class="btn btn-outline-primary" for="btncheck4">Java</label>

                    </div>


                    <!--Single file input for course thumbnail-->
                    <div class="mb-3">
                        <br>
                        <label for="formFile" class="form-label">Thumbnail cursus</label>
                        <input name="thumbnailCourse" class="form-control" type="file" id="formFile">
                    </div>

                    <!--Single file input for video-->
                    <div class="mb-3">
                        <br>
                        <label for="formFile" class="form-label">Video cursus</label>
                        <input name="file" class="form-control" type="file" id="formFile">
                    </div>

                    <!--                    Multiple files input for videos-->
                    <!--                    <div class="mb-3">-->
                    <!--                        <label for="formFileMultiple" class="form-label">Video's uploaden</label>-->
                    <!--                        <input class="form-control" type="file" id="formFileMultiple" multiple>-->
                    <!--                    </div>-->
                    <!---->
                    <!--                    Submit button-->
                    <button name="submit" type="submit" class="btn btn-primary" value="upload">Aanmaken</button>

                </form>
            </div>
        </div>
    </div>



</main>
<footer>
    <?php require 'utils/footer.php'; ?>
</footer>
</body>
</html>

1 Answers1

-1

This error happen when a column can't be null and does not have a default value.

Therefore, when you do an insert and this column is not included in the insert query, mysql assume null as insert value, but the query fail because the column an't be nulls not.

In your specific case the column that gives error is the ID one, probably you would define it as autoincrement column

Roberto Braga
  • 569
  • 3
  • 8
  • Before answering any basic question in after the year 2014, please search for duplicate pages. There are MILLIONS of pages on Stack Overflow. All basic questions are virtually guaranteed to be duplicates. Answering duplicates only rewards bad questions that shouldn't need asking if proper searching is done in the first place. The fundamental goal of closing duplicate questions is to help people find the right answer by getting all of those answers in one place. – mickmackusa Oct 17 '22 at 10:13