postgresql insert null value on query - php

In my php code query, i got some error when i will place a null value
into the table. The column names "number1","number2", and "number3" are integers and can have a null value.
if there is no null value, the query works
query is like this
insert into table(number1,number2,number3) values (1,2,3);
but when I leave a blank value on the input form on the PHP,
for example I will not place value for column "number2", the query will look
like this.
insert into table(number1,number2,number3) values (1,,3);
it says ERROR: syntax error at or near ","
SQL state: 42601
Does anybody have an idea how to solve this?
this query is likely to be the solution but is there any other way?
insert into table(number1,number2,number3) values (1,null,3);
because i got so many variables like that and am a bit lazy to place some conditions like when that variable returns a blank value then value="null"

You insert NULL value by typing NULL:
INSERT INTO table(number1,number2,number3) VALUES (1,NULL,3);
If you have a variable and when that variable is empty you want to insert a NULL value you can use NULLIF with the variable enclosed in single quotes to prepare for that (this is somewhat dirty solution as you have to treat the variable as an empty string and then convert it to integer):
INSERT INTO table(number1,number2,number3) VALUES (1,NULLIF('$var','')::integer,3);

Related

How to check if SQLite row is empty

I have the following which only inserts to column two if the value is empty:
UPDATE name
SET one=?,
two=COALESCE(two, ?),
three=?
WHERE id = ?
However, this only works if column two if the value is null. How can I get this to work if it is either NULL OR an empty string.
It should always insert to one and three. It should only insert to two if it is NULL or empty
I am using SQLite with PHP
If by "empty" you mean an empty string, you can use nullif():
two = COALESCE(NULLIF(two, ''), ?)
The more general solution is a CASE expression.
Don't store empty strings in TEXT columns.
Update the table to replace all empty strings with NULLs:
UPDATE name
SET two = NULL
WHERE TRIM(two) = ''
Now you can use your query, which is correct and in any other query you only need to check for NULL.

how to input single data in insert query

The error is:-
Field 'attn' doesn't have a default value41
If I set all the value of column query run perfectly like :
$stuattend = "INSERT INTO tblattendance(roll, attandence, att_time) VALUES('$roll', 'present', '5:30')";
I guess my problem is when I execute one value (roll) through query.
What would be the solution?
Hi the error clearly states that you have a column named as attn which is not nullable field by default. Either provide the default value or make it nullable or pass it some value so that it stops throwing the error.
You can change the above query to assign the default value as follows :
NOTE : Here you may assign the any value to it. Thinking that it may be integer I am doing as follows
INSERT INTO tblattendance(roll, attandence, att_time, attn) VALUES('$roll', 'present', '5:30', 10)

How to INSERT NULL value from PHP?

Let x is a column of the table t allowing NULL values.
Which value should $value variable take for the following PDO statement to insert NULL value?
$db->prepare("INSERT t SET x=:x")->execute(array(':x'=>$value));
You simply insert it as null:
$db->prepare("INSERT t SET x=:x")->execute(array(':x'=>null));
If you want, you can also add the data type of PDO::PARAM_NULL.
This was asked 9 years ago, so it may no longer be relevant, but SeanWM's existing answer, while it works, does not technically answer specifically what was asked because it does not assign any value to a variable.
Just for completeness:
Inserting the PHP null value into MySQL does result in NULL being inserted into the DB.
$value = null;
$sql = $db->prepare("INSERT t SET x=:x")->execute(array(':x'=>$value));
Output:
Note: be careful NOT to enclose the word null in quotes or it will insert the string "null" instead.

Enter either a date or a null value in database using MySql

I have searched the site and although I have found questions and answers similar I haven't been able to find an answer. After 4 hours of searching I've decided to bite the bullet and ask the question.
I have 4 date fields in a form that aren't required. I would like it to enter a date into the database if one of the fields has an entry or null if any are left blank.
I have an if statement that checks if the value is empty and if so $value = null, otherwise use $value = date("Y-m-d",strtotime($_post['value'])) to convert it to a date and this works well.
The problem is in my query. If I use '$value' it will insert the date correctly but won't insert a null value because using 'null' makes sql think it's a string. If I use just $value the null inserts just fine but the date goes in as 0000-00-00.
Any advice would be very much appreciated
Thanks for the advice so far...
Null is allowed, this is my script...
if(empty($_POST['fp32_original_install_date'])){
$fp32_install = NULL;
}else{
$fp32_install = date("Y-m-d",strtotime($_POST['fp32_original_install_date']));
}
$sql = "INSERT INTO accounts_cstm (id_c, support_c, install_date_c, sware_renewal_date_c, product_key_c, account_status_c, fp32_support_type_c, fp32_support_renewal_date_c, fp32_original_install_date_c) VALUES ('$Guid','$cdr_support', '$cdr_install', '$cdr_renew', '$prod_key', '$account_status', '$fp32_support', '$fp32_renew', $fp32_install)";
If I use in the query $fp32_install a null value goes in just fine but a date goes in as 0000-00-00, if I use '$fp32_install' the date goes in fine but a NULL value goes in as 1970-01-01 (probably because it sees 'NULL' as a string)
If I echo $fp32_install the value is shown as 2012-08-16 and the SQL type for the column is date and the default is NULL
If you are using posted values from a form, then $_POST['value'] will not be NULL.
You should check for empty values instead.
if($_POST['value']=="")
{
$value="NULL";
}
else
{
$value="'".date("Y-m-d",strtotime($_POST['value']))."'";
}
From the behavior you describe, it sounds as if your DATE column is defined with a DEFAULT 0 clause, or you are providing an invalid value.
According to the MySQL documentation:
<snip>
Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').
</snip>
It's difficult to diagnose the exact problem without seeing example code. As a starter, I suggest you try echoing out the SQL statement that is being sent to the database.
I have a strong suspicion that the value for the DATE column is going to appear with quotes around it, a string value of 'NULL', rather than the bare keyword NULL.

Zend: How to insert NULL values into MySQL

I am using Zend Framework with MySQL,Apache and Ubuntu 9.04.
I am trying to insert NULL values into database like this:
$personObj->setPersonId( '1' );
$personObj->setPersonEmail('NULL');
$personObj->save();
But 'NULL' is stored in database as string and not NULL.
When I use this:
$personObj->setPersonId( '1' );
$personObj->setPersonEmail(NULL);
$personObj->save();
But nothing happens and previous entry is unchanged.
What should I do to insert NULL values into MySQL?
If you are not modifying any of the values after they are assigned then
new $personObj->setPersonEmail(new Zend_Db_Expr('NULL'));
First thought would be straight passing in the null keyword, without quotes around it. As pavium said, the quotes around it turn it into a string.
I think putting NULL in quotes is what makes it look like a string.
I don't know about your method, but for a direct insert though a mysql INSERT command, the single quotes around a NULL are incorrect.

Categories