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

女生的网名这么多〃 提交于 2021-02-04 19:53:52

问题


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...


回答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).




回答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.




回答3:


It could be any problem related to the field. Check the HTML for spelling, confusion between name and value, etc. Your field may be empty because it is not finding the value you actually entered in the field because there is no such field per code (only on your screen). In my case, I had

$new_user = array( "id" => $_POST ['id'], "fname" => $_POST['firstname']

But the HTML was using fname and fname for both value and name.

And there was the first error: name="firstname" was written as vname="firstname" Then the confusion with name and HTML value.

You didn't post your HTML. The error is perhaps there.



来源:https://stackoverflow.com/questions/42647658/integrity-constraint-violation-1048-column-name-cannot-be-null-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!