MySQL statement always set value to 0 when UPDATE SET - php

My MySQL statement (wich I used 4 days to find a working one) SHOULD take 1 given value and check 3 different columns if they are filled or NULL. If the column got a value, it goes to the next column. If that column is NULL, it shall write the given value into that cell.
Unfortunately my statement is able to fetch that column, but when I try to SET the value, it is always set to 0. When i try to +1 the existing value it works with no problems.
Statement:
UPDATE `license` SET
`hardwareid1` = IF( `hardwareid1` = 123,`hardwareid1` = 987, `hardwareid1`) ,
`hardwareid2` = IF( `hardwareid2` = 123, `hardwareid2` = 987, `hardwareid2` )
WHERE `code` = 99
Where the = 123 is, in production I'll use IS NULL the 987 stands for the example value from "$value"
So if hardwareid1 is really 123 it doestn write 987 but 0 instead.
Please help

UPDATE license
SET hardwareID3 = CASE WHEN hardwareID1 IS NOT NULL AND
hardwareID2 IS NOT NULL AND
hardwareID3 IS NULL
THEN 987
ELSE hardwareID3 END,
hardwareID2 = CASE WHEN hardwareID1 IS NOT NULL AND
hardwareID2 IS NULL
THEN 987
ELSE hardwareID2 END,
hardwareID1 = CASE WHEN hardwareID1 IS NULL
THEN 987
ELSE hardwareID1 END
WHERE code = 99
SQLFiddle Demo
What the query does is when hardwareID1 IS NULL the new value 987 will be inserted on the column. At the same time, the value of hardwareID2 will also be changed if hardwareID1 has a value and hardwareID2 is null. Otherwise, all their value will retain.

I don't really know, but shouldn't your SQL look more like this?
`hardwareid1`=CASE
WHEN `hardwareid1`= NULL THEN set_to_xy

Related

How to insert a null value for INT in database?

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)

Execute multiple mysql updates in one query

What is the best way to merge two mysqli query's as one query
TABLE SERVER_JOINS
ID DEFAULT SERVER_ID MEMBER_ID
---------------------------------------
1 0 1 57
2 0 52 57
3 0 22 57
4 1 35 57
Only one row must have default as 1
By clicking on a link i want to change the default value
mysqli_query($database->connection,"UPDATE `server_joins` SET
`default` = '0' WHERE `default` = '1' AND `member_id` = '$session->u_id'");
mysqli_query($database->connection,"UPDATE `server_joins` SET
`default` = '1' WHERE `server_id`= '$id' AND `member_id` = '$session->u_id'");
I think you want to schieve something like that:
mysqli_query($database->connection,"UPDATE `server_joins`
SET `default` = '1'
WHERE `default`= '0' AND `server_id`= '$id' AND `member_id` = '$session->u_id'");
Use an if() function or a conditional case expression in the set clause depending on the value of the server field to decide whether default is to be set to 0 or 1.
UPDATE `server_joins`
SET `default` = if(`server_id`= $id, 1, 0)
WHERE `member_id` = $session->u_id
Couple of notes:
Your code is likely to be vulnerable to sql injection attack. Consider using prepared statements with parameters.
If you have numeric values in a field, then do not pass the values as string. Mysql has to convert the values on the flight.

how to replace database value by update table php

I have created two tables subscription_pending and subscription_complete
subscription_complete contain
id mag1 mag2 mag3 mag4
4 100 0 100 0
subscription_pending contain
id mag1 mag2 mag3 mag4
4 100
I have insert value by following command
$final_q= "INSERT INTO `subscription_complete` (`id`, `mag1`, `mag2`, `mag3`, `mag4`) VALUES ('".$row_q['id']."','".$row_q['mag1']."','".$row_q['mag2']."','".$row_q['mag3']."','".$row_q['mag4']."'
i want to update the subscription_complete table without replacing its value i.e. 100 for that i hvae write following query
$final_q1= "update subscription_complete set mag1='".$row_q['mag1']."',mag2='".$row_q['mag2']."',mag3='".$row_q['mag3']."',mag4='".$row_q['mag4']."' where id='".$row_q['id']."'";
can you tell me how to set 100 value in remaining column where the value is 0 ?
Thank you in advance !!
This assumes the empty fields in subscription_pending contain NULL, not empty strings.
UPDATE subscription_complete AS c
JOIN subscription_pending AS p ON p.id = c.id
SET c.mag1 = IFNULL(p.mag1, c.mag1),
c.mag2 = IFNULL(p.mag2, c.mag2),
c.mag3 = IFNULL(p.mag3, c.mag3),
c.mag4 = IFNULL(p.mag4, c.mag4)
If they're actually empty strings, use IF(p.magX = '', c.magX, p.magX) instead of the IFNULL test.

SQL checking for NULL fields

SOLVED: I knew there were fields that were empty that should have caught the check, but they were empty rather than NULL. So, when I was checking for NULL fields, it didn't find any.
I'm trying to check whether elements in a row with a specific name have any blank fields. I use it to find if any values are null, if there is, update and rows that were null. It isn't working properly though. I believe I have all the column names correct, I can't see what the problem is though. Here is my query:
//this goes through each row 1 by 1 and checks if an element is null
$stmt = $dbh->prepare("SELECT count(*) from csgo_item_list WHERE Item_Name =:itemname AND (Image_Link IS NULL OR Quantity IS NULL OR new_price IS NULL OR market_hash_name IS NULL OR last_update IS NULL OR is_tradable IS NULL OR old_price IS NULL OR item_type IS NULL OR skin_quality IS NULL OR skin_color IS NULL)");
//"$mydata->market_name" returns a valid name in the table
$stmt->bindValue(':itemname', $mydata->market_name);
$stmt->execute();
$count = $stmt->fetchColumn();
echo $count;
When I do this, some of the rows do have some null fields, yet when I echo $count it returns only 0's. This means I can't update my rows, because after the check I use the same line for an UPDATE:
if($count != 0){
$sql = $dbh->prepare("UPDATE csgo_item_list SET Quantity=:quantity, new_price=:newprice, Image_Link=:image_link, market_hash_name=:markethashname, last_update='1000-01-01 00:00:00', is_tradable='no', old_price=:oldprice, item_type=:type, , skin_quality=:skinquality, skin_color=:skincolor WHERE Item_Name=:itemname AND (Image_Link IS NULL OR Quantity IS NULL OR new_price IS NULL OR market_hash_name IS NULL OR last_update IS NULL OR is_tradable IS NULL OR old_price IS NULL OR item_type IS NULL OR skin_quality IS NULL OR skin_color IS NULL)");
I'll post an image of my database table, from what I've checked the names all match though:
http://gyazo.com/5ab3f2676c44eb696b02a38a64d9742a
Can anyone see why this isn't working?
I knew there were fields that were empty that should have caught the check, but they were empty rather than NULL. So, when I was checking for NULL fields, it didn't find any.
What I had to do was set all the empty columns to NULL:
UPDATE `table` SET `column` = NULL;
"....yet when I echo $count it returns only 0's. This mea...."
if($count != 0){ ....
if it is returning all '0's then why you are comparing "not equal" to 0 ?
it Should be if ($count == O){ I think.

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.

Categories