php 1048 列值不能为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24169525/
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
1048 Column value cannot be null
提问by user3731183
I am stuck, I cannot fix this error that I am getting, and I think it may be that my database options are messed up
我被卡住了,我无法修复我遇到的这个错误,我认为可能是我的数据库选项搞砸了
Here's where the data is put into a form
这是将数据放入表单的位置
<form name = "quoted" method="post" onsubmit="get_action(this);">
<input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual."> <br>
<textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
<div class = "checkboxes" required="required">
<h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
<label for="x"><input type="radio" name="x" value="Inspirational" id = "inspirational.php" checked="checked" /> <span>Inspirational</span></label><br>
<label for="x"><input type="radio" name="x" value="Funny" id = "funny.php" /> <span>Funny</span> </label><br>
<label for="x"><input type="radio" name="x" value="OutofContext" id = "outofcontext.php"/> <span>OutofContext</span></label>
</div>
<input id = "submit1" name="submit1"s type="submit"><br>
</form>
and here's the php to put that into the database
这是将其放入数据库的 php
<?php
$db_name = 'submissions';
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';
try {
$db = new PDO('mysql:host = localhost;dbname=submissions', $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$actual_quote = (isset($_POST['actual_quote']) ? $_POST['actual_quote'] : null);
$poster = (isset($_POST['poster']) ? $_POST['poster'] : null);
$sql = "INSERT INTO data (actual_quote, poster) VALUES ( :actual_quote, :poster)";
$query = $db->prepare($sql);
$query->execute(array(':actual_quote'=>$actual_quote, ':poster'=>$poster));
?>
( ! ) Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'actual_quote' cannot be null' in C:\wamp\www\Quotr\webweb2.php on line 113 ( ! ) PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'actual_quote' cannot be null in C:\wamp\www\Quotr\webweb2.php on line 113 Call Stack Time Memory Function Location 1 0.0015 257160 {main}( ) ..\webweb2.php:0 2 0.0206 267672 execute ( ) ..\webweb2.php:113
( ! ) 致命错误:未捕获的异常 'PDOException',消息为 'SQLSTATE[23000]:违反完整性约束:113 行 ( ) PDOException: SQLSTATE[23000]: 完整性约束违规: 1048 列 'actual_quote' 在 C:\wamp\www\Quotr\webweb2.php 中第 113 行调用堆栈时间内存函数位置 1 0.0015 257160 {main}() 不能为空..\webweb2.php:0 2 0.0206 267672 执行 ( ) ..\webweb2.php:113
When I do click null in the database, and I click the submit button on the form the error goes away and data shows up in the database, but there is no values, there's nothing there
当我在数据库中单击 null 并单击表单上的提交按钮时,错误消失并且数据显示在数据库中,但没有值,没有任何内容
can someone tell me whats wrong and how to fix this, if it's in the code, I personally think there's an issue with the database, here's some pictures of that.
有人可以告诉我出了什么问题以及如何解决这个问题,如果它在代码中,我个人认为数据库有问题,这里有一些图片。
(ignore the first 3 on the last picture, the last one is where I am at)
(忽略最后一张图片的前3个,最后一个是我所在的位置)
回答by Locke
Nothing is wrong with the database. It is doing exactly what it is supposed to be doing.
数据库没有任何问题。它正在做它应该做的事情。
When the database gives you those errors, it's telling you that you are trying to input "nothing" (null) into a column that doesn't allow it. You are sending nothing because if $_POST['actual_quote']
isn't set, it's null. Either get rid of that action if it isn't set or allow null in the database.
当数据库向您提供这些错误时,它告诉您您正在尝试将“无”(空)输入到不允许它的列中。您什么都不发送,因为如果$_POST['actual_quote']
未设置,则为空。如果未设置该操作,或者在数据库中允许为空,请删除该操作。
Also: get rid of the s
after the "
in name="submit1"s
.
另外:去掉ins
之后的。"
name="submit1"s
回答by Nawed Khan
You are setting $actual_quote as null when $_POST['actual_quote'] is not set... yet on the database side you have set this field to not allow NULL. Either allow NULL on database, or send empty string (two single quotes) when $_POST['actual_quote'] is not set (as shown below).
当 $_POST['actual_quote'] 未设置时,您将 $actual_quote 设置为 null...但在数据库端,您已将此字段设置为不允许 NULL。当 $_POST['actual_quote'] 未设置时,要么允许数据库为 NULL,要么发送空字符串(两个单引号)(如下所示)。
$actual_quote = (isset($_POST['actual_quote']) ? $_POST['actual_quote'] : '');
回答by Ziaur Rahman
I have faced the same problem. The 'field name' in view page and the column name of database was indifferent. My problem was:
我遇到了同样的问题。视图页面中的“字段名称”与数据库的列名称无关。我的问题是:
"Integrity constraint violation: 1048 Column 'MoneyMethod' cannot be null".
My view page input tag:
我的视图页面输入标签:
{!! Form::select("MoneyMethod", $MoneymethodInfo, null,["class"=>"form-control MoneyMethod required","id"=>"MoneyMethod"]) !!},
notice the spelling of 'MoneyMethod' in my database table $table->string('MoneyMethod', 100);
the spelling of money method is same as view page but in my controller
注意我的数据库表中“MoneyMethod”$table->string('MoneyMethod', 100);
的拼写,money 方法的拼写与视图页面相同,但在我的控制器中
$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.
仔细看“Moneymethod”的拼写与视图页面和数据库表列不同。更正拼写后,它现在可以工作了。
So, check the spelling of 'field name' and 'column name of database table' in form, controller, model and database table. It may solve your problem.
因此,请检查表单、控制器、模型和数据库表中“字段名”和“数据库表的列名”的拼写。它可能会解决您的问题。