php ERROR 1366 (HY000): 不正确的整数值:'' for column 'id' at row 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29462919/
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
ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1
提问by Arthur Siloka
My code:
我的代码:
$sql = "INSERT INTO ". static::$table_name ." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
output:
输出:
echo $sql;
//INSERT INTO users (id, username, password, first_name, last_name)
VALUES ('', 'lukeduke', '123456', 'Luke', 'Duke')
When I run this query, I get:
当我运行这个查询时,我得到:
ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1
ERROR 1366 (HY000): 不正确的整数值:'' for column 'id' at row 1
回答by Yanet Salazar Gutierrez
If you want to use this insert, you must provide an integer value in your sql instead of an empty string, it should by like this:
如果你想使用这个插入,你必须在你的 sql 中提供一个整数值而不是一个空字符串,它应该是这样的:
INSERT INTO users (id, username, password, first_name, last_name)
VALUES (1, 'lukeduke', '123456', 'Luke', 'Duke')
or if your id is autoincremental you can have your sql like this:
或者如果您的 id 是自动增量的,您可以像这样使用您的 sql:
INSERT INTO users (username, password, first_name, last_name)
VALUES ('lukeduke', '123456', 'Luke', 'Duke')
In this case, this should be your code:
在这种情况下,这应该是您的代码:
//extracting your first element of the array (id in this case)
$attributes = array_slice($attributes, 1);
$sql = "INSERT INTO ". static::$table_name ." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";
Read more at:
阅读更多信息:
回答by Mohammad
what is your id
column type?
你的id
列类型是什么?
if it is integer and auto increment, pass null
value to it
如果它是整数和自动递增,null
则将值传递给它