Integrity constraint violation: 1048 Column 'name' cannot be null error

43,833

Solution 1

The error seems quite clear. You have a column in the table that cannot take on a NULL value.

I would speculate that it is not one of the column where you are explicitly providing a value (name, surname, employment_date). You need to look at the definition of the table and look for another column defined as NOT NULL (or perhaps PRIMARY KEY with no default value).

Solution 2

Make sure that the column is marked as Primary key or auto_increment if you are using it that way, or mark it as null if you want to store null.

Share:
43,833
MKD
Author by

MKD

Updated on February 21, 2020

Comments

  • MKD
    MKD about 4 years

    there were a lot of answers related to this, but I couldn't find useful information. I'm trying to connect to the database and insert user's entered values into it, but I got this error and I seriously don't know what I am doing wrong. I've created 2 different classes in 2 different files, one is connection.php and the other is users.php (for insterting the users into the database) Could someone help me to solve this?

    Here is my connection.php file:

    <?php
    
    class Connection {
    public $dbh;
        // Setting Database Source Name (DSN)
    public function __construct() {
    $dsn = 'mysql:host=localhost;dbname=employees';
    // Setting options
     $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
    // Making the connection to the database
    try {
    $this->dbh = new PDO($dsn, 'root', '', $options); 
    }
    catch (PDOException $e) {
    $this->error = $e->getMessage();
            }
        }
    }
    $connection = new connection();
    ?>
    

    And here is my users.php file:

    <?php
     error_reporting(E_ALL);
    ini_set('display_errors', 1);
    include 'connection.php';
    class Users {
    public $name;
    public $surname;
    public $employmentDate;
    public $connection;
    public function __construct($connection)
    {
    $this->connection = $connection;
    if(isset($_POST['Submit'])) {
    $this->name = $_POST['name'];
    $this->surname = $_POST['surname'];
    $this->employmentDate = $_POST['employmentDate'];
    }
    }
    // Inserting users values to the database table
    public function insertUserValues() {
     $query= 'INSERT INTO employee (name,surname,employment_date)
     VALUES (:name,:surname,:employmentDate)';
     $stmt = $this->connection->dbh->prepare($query);
     $stmt->bindValue(':name',$this->name, PDO::PARAM_STR);
     $stmt->bindValue(':surname',$this->surname, PDO::PARAM_STR);
     $stmt->bindValue(':employmentDate',$this->employmentDate, PDO::PARAM_STR);
     $stmt->execute();
    }
    }
    $users = new Users($connection);
    $users->insertUserValues();
    ?>
    

    I got this error on users.php line 27, which is:

    $stmt->execute();

    And it says:

    Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

    I know here are a lot of code, but thanks if someone will try to help me...