MySQL error when submitting '' in an array - php

I have a PHP loop that's auto-generating an array to INSERT into a table. But I get this error:
Database query failed: Incorrect integer value: '' for column 'id' at row 1
Last SQL query: INSERT INTO users(id, email, password, first_name, last_name) VALUES ('', 'test#user.com', 'fljhdsfsd', 'John', 'Doe')
I've tried setting the id field as No Null and Null, but that didn't help.
Any ideas? Thanks in advance for your help.

If your id field is an auto-incremented column then you can simply omit it from the INSERT query.
INSERT INTO users(email, password, first_name, last_name)
VALUES ('test#user.com', 'fljhdsfsd', 'John', 'Doe');
An id will be generated automatically.

Your Mysql is running in the strict mode. You have to pass the NULL if you have no value for that column, if the column is auto increment you can keep out this column from query.
Once try the below query, I believe this will work.
INSERT INTO users(id, email, password, first_name, last_name)
VALUES (NULL, 'test#user.com', 'fljhdsfsd', 'John', 'Doe')

If id field is auto incremented you can skip that field. If not then read error, it says "Incorrect integer value" and you are giving '' witch is string. In this case before every insert you should fetch max id from database and increment it, or use any other ID generation.

This is happened because your id field in database now contains only integer value. and blanck is not a integer value. Just change the data type on your database.
Just change your datatype id "integer" to "varchar", tick out the "auto increment" and put a length like "250" field in database, then run your query.

Related

when i insert data its showing database error

When i was going to insert a student in my web app it was showing these error can anyone help me out.
Error Number: 1364
Field 'user_status' doesn't have a default value
INSERT INTO users (first_name, last_name, phone, profile_image, username, password, email, ip_address, created_on, last_login, active) VALUES ('dinesh', 'kumar', '+919591812528', 'fa07b9fc131dc4c7caadac44539c81c4.JPG', 'dinesh kumar', '$2y$08$8KVwr6dQWp8GtsXcFPrRsujqFIBGwdu9MEgMniAXdznXQifCHuDe2', 'dineshprivacy007#gmail.com', '49.207.177.226', 1503992683, 1503992683, 1)
Filename: /home/aspireng/aspiresms/models/ion_auth_model.php
Line Number: 872
user_status Column doesnt have a default value set, kindly set this field NULL as default, and then run query, or insert some value to this field.
if user_status field in your table, please set default value NULL if not assign.
This means there is a user_status column in you db which is expecting some value to be passed in. if you dont have any value to pass then do something like this
INSERT INTO users (first_name, last_name, phone, profile_image, username, password, email, ip_address, created_on, last_login, active,user_status) VALUES ('dinesh', 'kumar', '+919591812528', 'fa07b9fc131dc4c7caadac44539c81c4.JPG', 'dinesh kumar', '$2y$08$8KVwr6dQWp8GtsXcFPrRsujqFIBGwdu9MEgMniAXdznXQifCHuDe2', 'dineshprivacy007#gmail.com', '49.207.177.226', 1503992683, 1503992683, 1,'0')
your field user_status cannot be null so you need to pass any value in it. I just passed 0 for reference.

need assistance with solving this database error

i am trying to create a user account and i got this error message.
A Database Error Occurred
Error Number: 1048
Column 'group_id' cannot be null
INSERT INTO `bitauth_users` (`username`, `password`, `active`, `activation_code`, `group_id`, `password_last_set`) VALUES ('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233', NULL, '2016-06-23 04:07:08')
Filename: C:\inetpub\vhosts\vidoplus.com\httpdocs\system\database\DB_driver.php
Line Number: 330
Please what do i need to do to fix this. i am new at this. Thanks
As group_id itself suggest that it's a primary key of some table (I think its will be groups table) and treated as a foreign-key in your bitauth_users table.
So you can not pass NULL to it.
You have to pass some value which is related to the table where it is primary key.
Note:- please provide some real value (don't provide empty or 0 etc because at the time of fetching record you will face problem)
Form your error message, you need to give value of group_id (not null value). You can try this:
INSERT INTO bitauth_users (username, password, active, activation_code, group_id, password_last_set) VALUES ('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233', 1, '2016-06-23 04:07:08');
Hope it will help ;)
group_id is not null field ... so if you want to store NULL value then make it nullable other wise you can store 0 or '' blank value.
.
INSERT INTO `bitauth_users` (`username`, `password`, `active`, `activation_code`, `group_id`, `password_last_set`) VALUES ('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233', '', '2016-06-23 04:07:08')
In your sql,tha VALUES part,the group_id is setted to NULL.The error msg is "Column 'group_id' cannot be null".So you can convert NULL to zero.
The sql can be like this:
INSERT INTO `bitauth_users`
(`username`,`password`, `active`, `activation_code`, `group_id`,`password_last_set`)
VALUES
('FAAN', '0884e4e5928d672b182a37b885b5e6ec', 1, '7c6ef5bd05fd2d189df64da76e76017ecec7d233',
0, '2016-06-23 04:07:08')
Hope this can help you!

MYSQL - append or insert value into a column depending on whether it's empty or not

As title says, im trying to append a string to a VARCHAR column in my table.
The string is something like " //string ", forward slashes will be used later to explode the string to an array in PHP.
I was wondering if there's a way in MYSQL to perform a CONCAT(columnname, "//string") if the column is empty, otherwise perform a normal UPDATE ... SET ... WHERE . In this way, i will avoid the first value of my future exploded string to be a "//string" with forward slahes.
also, above I 've used bold characters for "in MYSQL" because I know i could first query the DB (to check if the column is empty) with something like:
$q = $conn->dbh->prepare('SELECT columnname FROM tablename WHERE username=:user');
$q->bindParam(':user', $username);
$q->execute();
$check = $q->fetchColumn();
and then leave PHP decide which operation perform:
if ($check != '') { // PERFORM A CONCAT }
else { // PERFORM AN UPDATE }
but this would mean a waste of time/resources due to 2x database calls and more PHP code.
thanks.
https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
That means in your case:
INSERT INTO tablename (id,columnname) VALUES (1,'//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
http://sqlfiddle.com/#!9/bd0f4/1
UPDATE Just to show you your options:
http://sqlfiddle.com/#!9/8e61c/1
INSERT INTO tablename (id, columnname) VALUES (1, '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES (1, '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES ((SELECT id FROM tablename t WHERE columnname='blahblah'), '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES ((SELECT id FROM tablename t WHERE id=2), '//string')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//string');
INSERT INTO tablename (id, columnname) VALUES ((SELECT id FROM tablename t WHERE columnname='newone'), '//newone')
ON DUPLICATE KEY UPDATE columnname=CONCAT(columnname,'//newone');
If what you want is this:
first string: column will contain 'firststring'
second string: column will contain 'firststring//secondstring'
then do the update like this:
UPDATE tablename SET columnname = CONCAT( IF(IFNULL(columnname,'')='','',CONCAT(columnname,'//')), :string) WHERE username=:user

Incorrect integer value for a column

When I submit null in center_id (not an auto increment column) in single quotes it give me the
error incorrect integer value for column center_id and when I write only the variable like this
center_id = $center_id
it gives a mysql error.
Please tell me what I am doing wrong with it or suggest something.
Below is my query.
Insert into parent_details set center_id='$center_id',name='$name',pin='$pin'
Thanks in advance.
Numbers and nulls should not be quoted, strings should. Additionally, your syntax for inserting is wrong:
INSERT INTO parent_details (center_id, name, pin)
VALUES ($center_id, '$name', $pin)
If you're using prepared statements you shouldn't have this problem in the first place:
$stmt = $db->prepare('Insert into parent_details
set center_id=:center, name=:name, pin=:pin');
$stmt->execute([
':center' => null,
':name' => 'foo',
':pin' => 'bar',
]);
field center_id data type is int but you may inserted string or special characters because of that is giving above error use below code try to insert integer to center_id column
insert into parent_details (center_id,name,pin) VALUES ($center_id, $name, $pin)

Sql Error - Incorrect integer value

I'm getting following error when I submit a form:
Can't added a new post. Incorrect integer value: '' for column 'aid' at row 1
Php Code:
$insert = mysql_query("INSERT INTO brt_articles VALUES( '', '$post_title', '$des',
'$date', '$org_date')");
if($insert)
{
echo "<font color=green>Successfully added a new article.</font>";
header("Refresh:3; url=allbrtarticle.php");
}
else
{
echo "<font color=red>Can't added a new post</font>" .
mysql_error();
}
In my Localhost It's ok. But in server why it's giving me a error message ?
Probably that DB has differents settings than your local. STRICT_TRANS_TABLES mode might be turned on.
Try SELECT ##GLOBAL.sql_mode; and SELECT ##SESSION.sql_mode;.
The aid field does not accept '' value as input.
The safest way is to specify column names as you are sending the query.
INSERT INTO brt_articles (title_field, description_field, date_field, org_date, fieldname) VALUES('$post_title', '$des', '$date', '$org_date');
If aid is a primary key, simply omit that field in your query
INSERT INTO brt_articles VALUES('$post_title', '$des', '$date', '$org_date')
aid is a column that is an integer, and you're trying to insert '' into it. '' is not a number, so the insertion fails.
Perhaps there's a server setting to auto-convert the incorrect type, but you shouldn't rely on it (as you've just found out)
This causes the error aid(INT) - which is included on you table columns(first column).
If its auto increment remove the ''.
$insert = mysql_query("INSERT INTO brt_articles VALUES('$post_title', '$des',
'$date', '$org_date')");
Regards
try
"INSERT INTO brt_articles VALUES('".$post_title."', '".$des."', '".$date."', '".$org_date."')"
Remove first '' it's not needed as MySQL automatically add it if its primary key and auto_increment.
your local db has the value of that column set to auto increment or has a default value.
on the server db, the table definition is not the same.
review and compare the table definitions and then make them consistent.

Categories