php 违反完整性约束:1048 列“名称”不能为空错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42647658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integrity constraint violation: 1048 Column 'name' cannot be null error
提问by MKD
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?
有很多与此相关的答案,但我找不到有用的信息。我正在尝试连接到数据库并将用户输入的值插入其中,但是我遇到了这个错误,我真的不知道我做错了什么。我在 2 个不同的文件中创建了 2 个不同的类,一个是 connection.php,另一个是 users.php(用于将用户插入数据库)有人可以帮我解决这个问题吗?
Here is my connection.php file:
这是我的 connection.php 文件:
<?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:
这是我的 users.php 文件:
<?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:
我在 users.php 第 27 行收到此错误,即:
$stmt->execute();
$stmt->execute();
And it says:
它说:
Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null
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...
我知道这里有很多代码,但是如果有人会尝试帮助我,谢谢...
回答by Gordon Linoff
The error seems quite clear. You have a column in the table that cannot take on a NULL
value.
错误似乎很清楚。表中有一列不能NULL
取值。
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).
我推测它不是您明确提供值 ( name
, surname
, employment_date
) 的列之一。您需要查看表的定义并查找定义为NOT NULL
(或者可能PRIMARY KEY
没有默认值)的另一列。
回答by Asuquo12
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.
如果以这种方式使用,请确保将列标记为主键或 auto_increment,如果要存储空值,则将其标记为空值。
回答by user2060451
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
它可能是与该领域相关的任何问题。检查 HTML 中的拼写、名称和值之间的混淆等。您的字段可能为空,因为它没有找到您在字段中实际输入的值,因为每个代码都没有这样的字段(仅在您的屏幕上)。就我而言,我有
$new_user = array(
"id" => $_POST ['id'],
"fname" => $_POST['firstname']
$new_user = array(
"id" => $_POST ['id'],
"fname" => $_POST['firstname']
But the HTML was using fname and fname for both value and name.
但是 HTML 使用 fname 和 fname 作为值和名称。
And there was the first error: name="firstname" was written as vname="firstname" Then the confusion with name and HTML value.
还有第一个错误:name="firstname" 被写成 vname="firstname" 然后是 name 和 HTML 值的混淆。
You didn't post your HTML. The error is perhaps there.
你没有发布你的 HTML。错误可能就在那里。