MySQL Laravel - SQL - SQLSTATE[01000]: 警告: 1265 数据在第 1 行列 'nsfw' 被截断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45018780/
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
Laravel - SQL - SQLSTATE[01000]: Warning: 1265 Data truncated for column 'nsfw' at row 1
提问by dwdawdawdaw
I'm trying to safe some data in my database and get the following error:
我正在尝试保护数据库中的一些数据并收到以下错误:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'nsfw' at row 1
SQLSTATE[01000]:警告:1265 数据在第 1 行的“nsfw”列被截断
The nsfw column has a 0 as a standart value. Thats my table:
nsfw 列的标准值是 0。那是我的桌子:
The nsfw column is also in the models $fillable
array.
nsfw 列也在模型$fillable
数组中。
I want to detect if a checkbox is checked. If it is checked, nsfw should be 1. If it's not, nsfw should be 0.
我想检测是否选中了复选框。如果选中,则 nsfw 应为 1。如果未选中,则 nsfw 应为 0。
Thats the checkbox HTML code:
这就是复选框 HTML 代码:
<div class="form-group">
<label for="nsfw" class="control-label">NSFW</label>
<br>
<input type="checkbox" name="nsfw">
</div>
And thats the controller code:
这就是控制器代码:
if (Input::get('nsfw')) {
$nsfw = true;
} else {
$nsfw = false;
}
// dd($nsfw) gives me true or
//false back, depending if the checkbox is checked or not
$thread = new Thread();
$thread->name = Input::get('thread-name');
$thread->description = Input::get('thread-desc');
$thread->age_min = Input::get('thread-min-age');
$thread->age_max = Input::get('thread-max-age');
if ($nsfw == true) {
$thread->nsfw = 1;
} else {
$thread->nsfw = 0;
}
$thread->save();
Thanks for your help and sorry for my bad english!
感谢您的帮助,并为我的英语不好而感到抱歉!
回答by Leo
Go to config/database.php
in the connections.mysql
set strict to false, then try to run it again, if you have no errors check if you have the data inserted correctly. If you don't then you are entering the wrong type of data to the mentioned column, so set your code to store values as strings:
转到config/database.php
在connections.mysql
一套严格的假,然后尝试再次运行它,如果你有任何错误检查您是否已经正确地插入数据。如果您不这样做,那么您在上述列中输入了错误类型的数据,因此将您的代码设置为将值存储为字符串:
if ($nsfw == true) {
$thread->nsfw = '1';
} else {
$thread->nsfw = '0';
}
回答by Alex
When strict mode is true and you get errors, it means you have other errors in the code, for example a wrong datatype or a spelling error.
当严格模式为 true 并且出现错误时,这意味着代码中有其他错误,例如错误的数据类型或拼写错误。
Leo's answer is right. I just want to emphasis, that you should reset the strict mode to true again.
狮子座的回答是对的。我只想强调,您应该再次将严格模式重置为 true。