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
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.
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….
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:
what is your id
column type?
if it is integer and auto increment, pass null
value to it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With