Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1 [closed]

Tags:

php

mysql

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

like image 885
Arthur Siloka Avatar asked Apr 05 '15 22:04

Arthur Siloka


People also ask

How do I fix error 1366 in MySQL?

To conclude, the ERROR 1366: Incorrect string value happens when MySQL can't insert the value you specified into the table because of incompatible encoding. You need to modify or remove characters that have 4-bytes UTF-8 encoding, or you can change the encoding and collation used by MySQL.

What is incorrect integer value?

Let me explain in code concept: If the string value is assigned to PHP script, that too the empty string is assigned meanly then you will get the error code Incorrect integer value….


2 Answers

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:

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:

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:

  • http://php.net/manual/en/function.array-slice.php
like image 64
Yanet Salazar Gutierrez Avatar answered Oct 24 '22 05:10

Yanet Salazar Gutierrez


what is your id column type?

if it is integer and auto increment, pass null value to it

like image 4
Mohammad Avatar answered Oct 24 '22 06:10

Mohammad