I want to set a field to null value by php variable.
I'm working with Laravel eloquent.
if($request->sectionId=='NULL'){
$sectionId=NULL;
}else{
$sectionId=$request->sectionId;
}
$question= new Question;
$question->sectionid=$sectionId;
It returns me error :
Incorrect integer value: 'NUll' for column farsifor_m.questions.sectionid at row 1 (SQL: insert into questions (formid, questionType, order, sectionid, updated_at, created_at) values (?, shortAnswer, 1, NUll, 2021-05-08 18:51:24, 2021-05-08 18:51:24))"
It seems you have a typo in the value of $request->sectionId - 'NUll' instead of 'NULL'.
Related
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!
I am using bindParam to set values for a MySQL insert. The column in question is an INT (11). It has a default of NULL and null is allowed. However, using bindParam it always receives a 0. I have confirmed that my $_POST['value'] is indeed null.
if(isset($_POST['value'])){
$stmt = $db->prepare("INSERT INTO table (column) VALUES (:column)");
$stmt->bindParam(':column',$_POST['value'], PDO::PARAM_INT);
$stmt->execute();
}
It keeps inserting a '0' instead of NULL if the POST value was ''.
You should be matching the complete case (Answer and type) with === (Read More)
Which most likely means that your value is not null like you presume it is.
Ensure it is by checking (pseudo code below):
if(VALUE !=== NULL) {
value = null
}
But you get the idea there? If not just comment :-)
And as aldanux mentioned in his comment, you have to wrap the column in backticks as it is a reserved word:
INSERT INTO table (`column`) VALUES (:column)
When I insert a NULL into a MYSQL INTEGER field via a PHP prepared statement it inserts as 0.
I have searched for similar questions and have update my code but am still having the problem.
In my code, if a text box sends an empty string to PHP; PHP converts it to NULL ie.
$v3 = (strlen($v3)<1 ) ? NULL : $v3;
As an example, a result in the UNIT column could be NULL.
The prepare statement
$stmt = $mysqli->prepare("INSERT INTO address ( `PERSON`, `TYPE`, `UNIT`, `STREET_NUM`, `STREET`, `STREET_TYPE`, `SUBURB`, `STATE` ) VALUES (?,?,?,?,?,?,?,?)"));
Bind the parameter as an integer
$stmt->bind_param('isiissss',$p, $add_type[$a], $unit[$a], $street_num[$a], $street_name[$a], $street_type[$a], $suburb[$a], $state[$a]);
In MYSQL, address.unit is NULLABLE and has no default value.
If I insert directly into the table and omit a value for the unit column; NULL is stored. Which I expected.
I suspect that the bind_param function changes NULL to 0 if the datatype for the column is specified as 'i' (integer), because NULLs passed to VARCHAR columns are stored as NULL.
Am I correct and if so how should I pass a NULL to the integer column?
To simplify my question (and because I didn't think it would be relevant) I omitted that the values where passing through a $mysqli->real_escape_string function and after some testing I found that it converts a NULL to an empty string
$test = "";
$test = (strlen($test)<1 ) ? NULL : $test;
var_dump($test); // null
echo "<br>";
$test = $mysqli->real_escape_string($test);
var_dump($test); // string '' (length=0)
This does not solve my problem but it does answer this question
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.
Sometimes I need to insert into the table some null values, or update them setting the value to NULL.
I've read somewhere in the Postgres documentation that this can't be done, but can be tricked with the default value:
pg_query("INSERT INTO my_table (col_a, col_b) VALUES ('whatever', default)
I know that in this example I'll have the same result with:
pg_query("INSERT INTO my_table (col_a) VALUES ('whatever')
But the problem comes with prepared statements:
pg_prepare($pgconn, 'insert_null_val', "INSERT INTO my_table (col_a, col_b) VALUES ($1, default)");
pg_exec($pgconn, 'insert_null_val', array('whatever'));
//this works, but
pg_prepare($pgconn, 'insert_null_val', "INSERT INTO my_table (col_a, col_b) VALUES ($1, $2)");
pg_exec($pgconn, 'insert_null_val', array('whatever', 'NULL'));
//insert into the table the string 'NULL'.
//instead using array('whatever', '') it assume the col_b as empty value, not NULL.
The same problem applies to update statements.
I think there is a solution, because pgmyadmin can do that (or it seems like it can).
If you are wondering why I need to play with null values in my tables, let me throw an example (maybe there is a way better then the null value?):
Assume I have the users table with an email column, which can be empty, but has a unique index. 2 empty emails are equal and violate the unique constraint, while 2 NULL values are not equal and can coexist.
Use the php's literal NULL as a parameter:
pg_prepare($pgconn, 'insert_null_val', "INSERT INTO my_table (col_a, col_b) VALUES ($1, $2)");
pg_query($pgconn, 'insert_null_val', array('whatever', NULL));