PHP MYSQL Update multiple fields including DATETIME field - php

I can update the field 'delivered' from 0 to 1 no problem but when I try to update 'delivered_time' nothing happens in the database. This may be to do with the way I am writing the code but If anybody could help I would really appreciate it, thanks!
if (isset($_POST['delivered']) === false) {
mysql_query("UPDATE `listings` SET `delivered_time` = '{$date->format('Y-m-d H:i:s')}' AND `delivered` = 1 WHERE `order_id` = $order_id");
}
I have also tried this but this did not work either
if (isset($_POST['delivered']) === false) {
mysql_query("UPDATE `listings` SET `delivered_time` = NOW() AND `delivered` = 1 WHERE `order_id` = $order_id");
}
My MYSQL database is set to have 'delivered' defined to 0 and is stored as an INT value. The 'delivered_time' field is stored as a DATETIME value in the database.

, not AND as the field separator; AND is used in WHERE clauses
UPDATE `listings`
SET `delivered_time` = '{$date->format('Y-m-d H:i:s')}',
`delivered` = 1
WHERE `order_id` = $order_id

Try this code
<?php
if (isset($_POST['delivered']) === false) {
mysql_query("UPDATE `listings` SET `delivered_time` = NOW(),`delivered` = 1 WHERE `order_id` = ".$order_id);
}
?>

Related

Unable to catch null data properly from mysql

I'm trying to catch all data from MySQL to check data already exists or not
if data already exists then it should update else insert new data.
I have 7 to 8 conditions in where clause, some of them are null
now the issue is
when i run query its not catching null column thats why its insert new data instead of updating
below is my code
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
if (mysqli_num_rows($fetch_stock) > 0) {
echo "UPDATE book_stock SET stock_count = stock_count + 1 WHERE book_name = '$book_name' AND author_id = $book_author AND author_id1 = $book_author1 AND author_id2 = $book_author2 AND author_id3 = $book_author3 AND author_id4 = $book_author4 AND author_id5 = $book_author5 AND author_id6 = $book_author6";
mysqli_query($connection,
"UPDATE book_stock
SET stock_count = stock_count + 1
WHERE book_name = '$book_name'
AND author_id = $book_author
AND author_id1 = $book_author1
AND author_id2 = $book_author2
AND author_id3 = $book_author3
AND author_id4 = $book_author4
AND author_id5 = $book_author5
AND author_id6 = $book_author6"
);
} else {
echo "INSERT INTO book_stock (book_name, author_id, author_id1, author_id2, author_id3, author_id4, author_id5, author_id6, stock_count) value ('$book_name', $book_author,$book_author1, $book_author2, $book_author3, $book_author4, $book_author5, $book_author6, 1)";
mysqli_query($connection,
"INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2,
author_id3, author_id4, author_id5, author_id6,
stock_count)
value ('$book_name', $book_author,$book_author1, $book_author2,
$book_author3, $book_author4, $book_author5, $book_author6, 1)");
}
When i print query Its look like this
INSERT INTO book_stock
(book_name, author_id, author_id1, author_id2, author_id3,
author_id4, author_id5, author_id6, stock_count)
value ('cat demo1', 1,2, 3, 4, NULL, NULL, NULL, 1)
I got solution for this from 1 website, which is
$fetch_stock = mysqli_query($connection,
"SELECT *
FROM `book_stock`
WHERE book_name = '$book_name'
AND author_id = $book_author
AND IFNULL(author_id1, 0)= IFNULL($book_author1, 0)
AND IFNULL(author_id2, 0)= IFNULL($book_author2, 0)
AND IFNULL(author_id3, 0)= IFNULL($book_author3, 0)
AND IFNULL(author_id4, 0)= IFNULL($book_author4, 0)
AND IFNULL(author_id5, 0) = IFNULL($book_author5, 0)
AND IFNULL(author_id6, 0) = IFNULL($book_author6, 0)"
);
Its working when i want to display stock.
But i want to do a crud operation by using this logic.
I tried to do from above solution, its work fine but, in db table its update null with 0.
which is not got as per me.
please guide me with best solution.
Thanks

Feedback for php code

hello guys I am subtracting one unit from a variable called Eppl which resides in a Database and this is working perfectly. However I have added a condition which is that the value of the variable Eppl must be greater than 0 before the subtraction takes place. My code is as follows:
$id = $_GET["id"];
$result = $mysqli->query("SELECT `CreatedBy` AND `Eppl` FROM `events` WHERE `Eid` = '$id'");
$row = $result->fetch_assoc();
$sessionid = $row['CreatedBy'];
$notlesszero = $row['Eppl'];
$zero = 0;
//echo $result;
session_start();
if ((!($_SESSION['login_user'] == $sessionid)) && ($notlesszero>0)){
$mysqli->query("UPDATE `events` SET `Eppl` = `Eppl` - 1 WHERE `Eid` = '$id'");
$mysqli->query("INSERT INTO `eventusers` (EventID, UserID) VALUES ('{$id}', '{$_SESSION['login_user']}')");
header("location:view.php");
//}
}
The if branch works without the second condition $notlesszero>0...
What is the problem here?
Thanks for your help
Instead of querying the database to get the value of the field and then checking it, why not just add a condition on the UPDATE?
$mysqli->query("UPDATE `events` SET `Eppl` = `Eppl` - 1 WHERE `Eid` = '$id'");
to
$mysqli->query("UPDATE `events` SET `Eppl` = `Eppl` - 1 WHERE `Eid` = '$id' AND `Eppl` > 0");
The first query is wrong after you added Eppl to it. Fields to select should not be separated by AND. So
SELECT `CreatedBy` AND `Eppl` FROM
should be
SELECT `CreatedBy`, `Eppl` FROM
This is something you could have caught by inspecting the error information after executing the query, or at least by inspecting $row after changing the query. :-p

Error updating mysql

I am sending data from an android app to save in my mysql database.
My Codes are
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = $rem WHERE transaction_id = $t_id LIMIT 1");
And my dabase is as,
remark varchar(500) latin1_general_ci.
The problem is whenever I have $rem as a numbric value then database is updated but if I use any text then it wont.
Please help me.
Make sure wrapping string data with single quote.
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = '$time', transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1")
Text should be wrapped by single quotes: ''
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1");

How to update a value by 1 if the new value inserted into the database clashes with value in the database?

I want to update the database of the sort order column to increase its value by one if the the new value inserted into the database clashes with the value that is already in the database. May I know how should I go about doing it? Please help! Thanks!
Below is my code (I am not sure whether am I on the right track):
$result = mysql_query("SELECT sortorder FROM information ORDER BY id ASC;");
if($result >= 1 ){
$i=1;
while ($initialorder = mysql_fetch_assoc($result))
{
$initialorder = $initialorder["sortorder"];
if ($sortorder == $initialorder ){
$result6 = mysql_query("SELECT * FROM information
WHERE `sortorder` = '$sortorder'");
$row6 = mysql_fetch_array($result6);
$removethis1 = $row6['id'];
$result7 = mysql_query("UPDATE information
SET `sortorder`= ((SELECT `sortorder`
FROM (SELECT MAX(`sortorder`) AS
'$initialorder' FROM information) AS '$initialorder') + 1)
WHERE id='$removethis1'");
}
$query = "INSERT INTO `information`
(`id`,`page`,`description`,`status`,`sortorder`,`keyword`,`date_added`)
VALUES
('$id','$title','$description','$status',
'$sortorder','$keyword','$date_added')";
$result = mysql_query($query, $conn);
header('Location: index.php?status=1&title='.$title);
$i++; }
}
You can do this:
INSERT INTO ON `information`
...
DUPLICATE KEY UPDATE
sortorder = '".$sortorder + 1." '

Update Query not updating field in table

I am currently struggling with the following :
$res = $db->uniquequery("SELECT distinct won_current,ctt,darkmatter FROM ".USERS." WHERE `id` = '".$USER['id']."'");
$checkwon = $res['won_current'];
$ct=$res['ctt'];
$dm=$res['darkmatter'];
These appear to be working. Now...
$dmgift=0;
while($checkwon>=1000)
{
$ct=$ct+1;
//incrementing $ct doesnt seem to work in the loop but it works fine outside it
$checkwon=$checkwon-1000;
//threshold ranges and setting of dmgift.
switch($ct){
case $ct=1 : $dmgift=25000;break;
case $ct>=5 && $ct<10: $dmgift=50000;break;
case $ct>=10 && $ct<15: $dmgift=75000;break;
case $ct>=15 && $ct<20: $dmgift=100000;break;
case $ct>=20 : $dmgift=150000;break;
}
$dm=$dm+$dmgift;
//$db->query("UPDATE ".USERS." SET won_current=$checkwon,ctt=$checkthreshold,darkmatter=$dm WHERE `id` = ".$USER['id'].";");
$db->query("UPDATE ".USERS." SET won_current=$checkwon WHERE `id` = ".$USER['id'].";");
$db->query("UPDATE ".USERS." SET ctt='$ct' WHERE `id` = ".$USER['id'].";"); // this update query is not passing.db field remains the same
$db->query("UPDATE ".USERS." SET darkmatter=$dm WHERE `id` = ".$USER['id'].";");
}
Kindly advise...the other 2 update queries are passing well...if you notice above the 3 updates I had a full query commented in...split it to see what is not working in update...
I did echo and vardump...outside the loop they gave me the correct values...BUT inside the loop they have me 11111 instead of 5...for example...not sure what I'm doing wrong....I'm very new to php ( 2 days ? ) and would appreciate a solution...
The problem is with the switch.
switch($ct){
case $ct=1 : $dmgift=25000;break;
...
}
The first case changes $ct to 1, so its value is always 1 in the SQL query so it looks like the query doesn't work.
In any case switch doesn't work like that, you need separate if phrases:
if( $ct == 1 ) {
$dmgift=25000
}
else if( $ct>=5 && $ct<10 ) {
$dmgift=50000;
}
else if( $ct>=10 && $ct<15 ) {
$dmgift=75000;
}
else if( $ct>=15 && $ct<20 ) {
$dmgift=100000;
}
else if( $ct>=20 ) {
$dmgift=150000;
}
Also note that you don't have cases for $ct between 2 and 4.
Check for $ct value.
If $checkwon is not greater than or equal to 1000, $ct value will remain the same db value
$db->query("UPDATE ".USERS." SET ctt='" . $ct . "' WHERE `id` = ".$USER['id'].";");
Change your update query
$db->query("UPDATE ".USERS." SET won_current = '".$checkwon."' WHERE `id` = '".$USER['id']."'");
$db->query("UPDATE ".USERS." SET ctt = '".$ct."' WHERE `id` = '".$USER['id']."'");
$db->query("UPDATE ".USERS." SET darkmatter = '".$dm."' WHERE `id` = '".$USER['id']."'");
Remove ";" semicolon from here
use single quotes for values of MySQL
$db->query("UPDATE ".USERS." SET won_current='$checkwon' WHERE id = '".$USER['id']."'");

Categories