php SQLSTATE[23000]:违反完整性约束:1048 列“post”不能为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20058252/
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
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post' cannot be null
提问by Baran Sengul
I have this codes but when I try these code I am getting:
我有这些代码,但是当我尝试这些代码时,我得到:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post' cannot be null this error.
SQLSTATE[23000]:违反完整性约束:1048 列“post”不能为空此错误。
I could not find any solution. I tried another functions which are work they are worked but this one is not work.
我找不到任何解决方案。我尝试了另一种可以工作的功能,但是这个功能不起作用。
?>
<form method="post">
Buraya yaz <br><textarea style="resize:none" name="post"></textarea> <br><input type="submit" value="G?nder">
</form>
<?php
if(empty($_POST['post'])){
echo 'you should fill the area.';
exit;
}
if (empty($errors) === true) {
$users->sendpost($post);
}
?>
public function sendpost($post){
$query = $this->db->prepare("INSERT INTO `posts` (`post`) VALUES (?) ");
$query->bindValue(1, $post);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
采纳答案by liam.derossi
insert $post = $_POST['post'];
插入 $post = $_POST['post'];
$post = $_POST['post'];
if(empty($_POST['post'])){
echo $errors[] = 'you should fill the area.';
}
if (empty($errors) === true){
$users->sendpost($post);
}
回答by Ziaur Rahman
I have face same problem. The 'field name' in view page and the column name of database was different. My problem was:
"Integrity constraint violation: 1048 Column 'MoneyMethod' cannot be null".
My form input tag:{!! Form::select("MoneyMethod", $MoneymethodInfo, null,["class"=>"form-control MoneyMethod required","id"=>"MoneyMethod"]) !!}, notice the spelling of 'MoneyMethod' and in my database table $table->string('MoneyMethod', 100);the spelling of 'MoneyMethod' is same as form but in my controller $riskfund->Moneymethod = Input::get('Moneymethod');look carefully the spelling of 'Moneymethod' differs from view page and database table column. After correcting the spelling, it is working now.
So, please check the spelling of 'post' in form, controller, database table. it may help you.
我面临同样的问题。视图页面中的“字段名”和数据库的列名不同。我的问题是:
"Integrity constraint violation: 1048 Column 'MoneyMethod' cannot be null"。我的表单输入标签:{!! Form::select("MoneyMethod", $MoneymethodInfo, null,["class"=>"form-control MoneyMethod required","id"=>"MoneyMethod"]) !!}注意“MoneyMethod”的拼写,在我的数据库表$table->string('MoneyMethod', 100);中“MoneyMethod”的拼写与表单相同,但在我的控制器中$riskfund->Moneymethod = Input::get('Moneymethod');仔细查看“Moneymethod”的拼写与视图页面和数据库表列不同。更正拼写后,它现在可以工作了。因此,请检查表单、控制器、数据库表中“post”的拼写。它可能会帮助你。
回答by Francisco Presencia
One of the problems is that you're displaying the error but not doing anything about it. Change it to:
问题之一是您正在显示错误但没有做任何事情。将其更改为:
if(empty($_POST['post'])){
echo 'you should fill the area.';
exit;
}
However, as the main recommendation, you should use exceptions that can be catched:
但是,作为主要建议,您应该使用可以捕获的异常:
if(empty($_POST['post'])){
throw new Exception("You should fill the area.");
}
This is not the only error, as noted in the comments by @RocketHazmat . If you didn't understand what he's saying, you should do $users->sendpost($_POST);instead of $users->sendpost($post);.
正如@RocketHazmat 在评论中指出的那样,这不是唯一的错误。如果你不明白他在说什么,你应该做$users->sendpost($_POST);而不是$users->sendpost($post);。

