How to insert a null value for INT in database? - php

I am not using HTML form to get values for this query. I am just inserting a second query once user creates a specific record. However, the second query is not inserting '0' unless I change it to VARCHAR. Others are working fine except for number.
$null = null;
$query2="insert into npi_program2 (prodID,prodName,TPM,scheme,phases,status,number,date,remarkProg)
values('$prodID','$prodName','$null','$null','$null', 0 ,DATE_FORMAT(NOW(),'%b-%d-%Y'),'$null')";
$res1=$db->query($query2);

0 is not equal to null. 0 is false. To insert null in the database, you should pass NULL as a value.
$query2="insert into npi_program2 (prodID,prodName,TPM,scheme,phases,status,number,date,remarkProg)
values($prodID,$prodName,NULL,NULL,NULL, 0 ,DATE_FORMAT(NOW(),'%b-%d-%Y'),NULL)";
$res1=$db->query($query2)

Related

Insert into field if the field is NULL mysql

I am trying to check if a value column is null then insert a value into it based of a series of if statements, my only problem is I don't know what the value of null actually is in PHP.
I have checked that the id_created is being echo'd out, and the first insert script is working but it seems there is problem with my if statement:
if (isset($_POST['ra'])){
$db->query('INSERT table (`lid`, `id_created`, `call_start`) VALUES ('.intval($_GET['id']).','.intval($_SESSION['user']['id']).',NOW())');
if ($_SESSION['user']['id'] == $calllogs['id_created']){
if ($calllogs['call_end'] == 'Null'){
$db->query('INSERT INTO table (`call_end`) VALUES (NOW()');
}
}
}
Thanks

MySQL Where clause values 0 and 1 not working correctly

The situation
In table I have columns configurated as ENUM('0','1'). I have select query build with PDO like this example
$value = isset($_POST['value']) ? $_POST['value'] : (isset($_GET["value"]) ? $_GET["value"] : null);
$sql = $pdo->prepare("SELECT * FROM tablename WHERE column = :value");
$sql->bindValue(':value', $_POST['value']); // post contains 0 or 1
$sql->execute();
The problem
When printing the results, value 1 is working normally. But when using value 0, all rows are showing including rows with value 1.
Following query is working normally when trying it in HeidiSQL, but it's not with PHP. What's wrong? SELECT * FROM tablename WHERE column = '0'
I noticed that PHP thinks $_POST['value'] is unset when its value is zero. I'm using isset()
Trying to solve the problem
No effect either if using $_GET['value'] and url like index.php?value=0
Tried following, not working $sql->bindValue(':value', '0'); // post contains 0 or 1
I changed column type to TINYINT(1) - no effect. When looking for zero, all are showing.
Set PDO bindValue() $data_type to PDO::PARAM_BOOL, not working
Try this:
$value = empty($_POST['value']) ? '0' : '1' ;
$sql = $pdo->prepare("SELECT * FROM tablename WHERE column = :value");
$sql->bindValue(':value', $value, PDO::PARAM_STR); // post contains 0 or 1
$sql->execute();
Good luck!
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.
An enumeration value must be a quoted string literal; it may not be an expression, even one that evaluates to a string value.
The index value of the empty string error value is 0. This means that
you can use the following SELECT statement to find rows into which
invalid ENUM values were assigned:
mysql> SELECT * FROM tbl_name WHERE enum_col=0;
So use single quote to consider the zero(0) as string.

Integer inserting as 0 instead of NULL

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

SELECT statement for NOT NULL values is returning NULL values.

I have PHP code that gets values using a $_POST and then inserts into a DB. All of the fields are input based except the CU00, etc. because it denotes a primary key .
Now suppose user enters just one row:
$ sql="INSERT INTO weekly
VALUES
('$_POST[uactual]','$_POST[utarget]','CU001','$a1','$_POST[ucomment]',NOW())
,('$_POST[uactual2]','$_POST[utarget2]','CU002','$a2','$_POST[ucomment2]',NOW())
,('$_POST[uactual3]','$_POST[utarget3]','CU003','$a3','$_POST[ucomment3]',NOW())
,('$_POST[dactual]','$_POST[dtarget]','CD001','$b1','$_POST[dcomment]',NOW())
,('$_POST[dactual2]','$_POST[dtarget2]','CD002','$b2','$_POST[dcomment2]',NOW())
,('$_POST[dactual3]','$_POST[dtarget3]','CD003','$b3','$_POST[dcomment3]',NOW())
,('$_POST[iactual]','$_POST[itarget]','CI001','$c1','$_POST[icomment]',NOW())
,('$_POST[iactual2]','$_POST[itarget2]','CI002','$c2','$_POST[icomment2]',NOW())
,('$_POST[iactual3]','$_POST[itarget3]','CI003','$c3','$_POST[icomment3]',NOW())
,('$_POST[ractual]','$_POST[rtarget]','CR001','$d1','$_POST[rcomment]',NOW())
,('$_POST[ractual2]','$_POST[rtarget2]','CR002','$d2','$_POST[rcomment2]',NOW())
,('$_POST[ractual3]','$_POST[rtarget3]','CR003','$d3','$_POST[rcomment3]',NOW())";
SQL TABLE
ACTUAL|TARGET|KEY |SIGNAL |TIME
NULL NULL CU001 NULL 00:00
NULL NULL CU002 NULL 00:00
NULL NULL CU003 NULL 00:00
NULL NULL CU004 NULL 00:00
100 200 CU005 300 00:00
I want to do a select where only the rows with signal are selected. But when I do a:
SELECT *
FROM TABLE
WHERE
'signal' IS NOT NULL
I get all the rows returned. It is as if there are no NULL values in my table .
SELECT *
FROM TABLE
WHERE
signal IS NOT NULL
'signal' is just string literal which is indeed NOT NULL.
You possibly meant ` instead of '.
You are putting empty strings into the DB.
Do this:
(empty($_POST['uactual']) ? 'NULL' : '\'' . $_POST['uactual'] . '\'')
For all $_POST variables. Then you can be sure that there will be NULL values.
Please don't do this: $_POST[uactual]! PHP will search for defined val first! Do $_POST['uactual'] Use ' always!
Also, your select is wrong. ` not '. If you don't believe do such query:
SELECT 'signal' FROM TABLE
This is correct:
SELECT `signal` FROM TABLE or
SELECT signal FROM TABLE
This will return you string 'signal', not the `signal` column.
WHERE
'signal' IS NOT NULL
This will always be true! This why it returns whole data. You made 2 mistakes then :] In inserting and in selecting data.
You are not inserting any NULLs. You are inserting empty strings at best.
Also, you should use some validation of your inputs.

Filling NULL value again on deletion or updation

I have updated certain field in my database & that field has default value as NULL. How can I again fill NULL if field is emptied or deleted. As during this updation blank value is filled in database and of course that is not NULL. Is filling NULL instead of blank value good?
Using php how can I do that? Suppose I have condition if code
if(!empty($photo))
{ $a in database;
}
But in this condition if $photo is empty it will fill blank value of $a... Let me know to fill NULL in database.
You just have to write null, and not an empty value, to your database.
You SQL query would look like this :
update your_table
set your_field = NULL
where ...
instead of :
update your_table
set your_field = ''
where ...
To switch between those two queries, you might need some condition in your PHP code, depending on how it's organized ; maybe like this :
if (empty($photo)) {
// query to set the field to NULL
}
else {
// query to update the value
}
Note that if you have no value to store for a row in your database, you might also want to just delete that row :
delete from your_table
where ...
Of course, it's up to you to determine which is the best for your application : a row with a NULL value, or no row.

Categories