How can I update with a NULL value - php

I am trying to update a mysql table. If there is no value I want null but I'm getting 0 if $ODate is empty. Actually I'm getting 0000-00-00. What I need to do is get rid of the ' ' then it will take the NULL value but then it doesn't work if there is a valid date value.
if (($_POST['OrderDate'])==null){$ODate='NULL';}else {$ODate=$_POST['OrderDate'];}
//this will update a valid date but returns 0000-00-00 if set to NULL
"UPDATE tblLoads SET OrderDate = '$ODate',
//this will update NULL but returns 0000-00-00 if a valid date
"UPDATE tblLoads SET OrderDate = $ODate,

$ODate = empty($_POST['OrderDate']) ? "NULL" : "'$_POST[OrderDate]'";
$query = "UPDATE tblLoads SET OrderDate = $ODate, ...";

You could have an if else, checking for if $ODate is NULL, if it is then don't use the quotes, if it isn't null, then use the quotes.
e.g.,
if (!is_null($ODate)) {
$ODate = "'" . $ODate . "'";
}
"UPDATE tblLoads SET OrderDate = $ODate,

Related

php - quoted date is subtracting

I don't know why this is happening but I have already quoted my date and everytime I run this code, instead of updating dates, it's just subtracting them and returning:
0000-00-00
Here is my code:
$format_date = date("Y-m-d", strtotime($date));
$newinfo = "UPDATE
tickets
SET
`date` = '".mysqli_real_escape_string($connct,$format_date)."'
AND `boolean` = '".mysqli_real_escape_string($connct,$booleancheck)."'
WHERE
`id` = '".mysqli_real_escape_string($connct,$id)."'
";
$update_this = mysqli_query($connct, $newinfo);
EDIT:
Thanks to #Barmar, I used ,instead of AND and it worked.
$newinfo = "UPDATE
tickets
SET
`date` = '".mysqli_real_escape_string($connct,$formatar_data)."',
`boolean` = '".mysqli_real_escape_string($connct,$booleancheck)."'
WHERE
`id` = '".mysqli_real_escape_string($connct,$id)."'
";

Why does this MySQL Update query not work?

Here is my code for updating the date field for all records that have the field WorkoutID = $currentWorkoutID. When I run the query the dates change to 0000-00-00 and not to the current date. How can I fix this? Btw DB::getInstance() executes the query. I think something is wrong with the actual query?
$currentWorkoutID = $_SESSION['GlobalWorkoutID'];
echo $currentWorkoutID;
$date = date("y/m/d");
echo $date;
$sql = "UPDATE workout SET Date = ".$date." WHERE WorkoutID = ".$currentWorkoutID."";
DB::getInstance()->query($sql);
include single quotes ' for date
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
^ ^
i agrre with the answer
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
because maybe the type of Date is String ,
hope to help you,thank you
Try this
$sql = "UPDATE workout SET `Date` = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
Because date is a reserved keyword

PHP MYSQL Update multiple fields including DATETIME field

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);
}
?>

Cannot save php datetime in database

I want to save a datetime from PHP into my database, but I cannot. The datetime value in the database is always set as 0000-00-00 00:00:00. What am I doing wrong?
My code:
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = $dt, post_date_gmt = $dt WHERE ID = $id";
try put quotation marks:
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = '$dt' WHERE ID = $id";
Simply, datetime is string, not integer.
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = $dt WHERE ID = $id";
will be works better:)
You can just use,
$dt = date("Y-m-d H:i:S");
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = '$dt' WHERE ID = $id";
Instead of using date time from PHP you should use mysql inbuit now() function, no need to write extra line of code in php.
$sql = "UPDATE wp_posts SET post_date = now(), post_date_gmt = now() WHERE ID = $id";
n you can alos set Now() in your table field so you dont need to set as query , when record will insert to update it will automatically set the current date time.
CREATE TABLE tablename
(
fiedlId int NOT NULL,
fieldName varchar(50) NOT NULL,
fieldDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (fiedlId)
)
Cheers!!
why don't you use timestamp function of mysql
for new table
$sql = CREATE TABLE newtb (current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
or
$sql = CREATE TABLE newtb (current_time DATETIME DEFAULT NOW());
to modify Table
$sql = CREATE TABLE newtb MODIFY current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
or
$sql = CREATE TABLE newtb MODIFY current_time DATETIME DEFAULT NOE();

Why the script saves all the time the same data?

I have a problem with my php Script that the system runs every minute.
My php-script is this:
<?php
$y = date("Y", time());
$m = date("m", time());
$d = date("d", time());
$h = date("H", time());
mysql_connect("localhost", "root", "");
mysql_select_db("dashboard");
$check_date = mysql_query("SELECT year,month,day FROM serverstats WHERE year='".$y."' && month='".$m."' && day='".$d."'");
if(mysql_num_rows($check_date)==0) {
mysql_query("INSERT INTO serverstats (year,month,day,h01,h02,h03,h04,h05,h06,h07,h08,h09,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23,h24)
VALUES (
'".$y."', '".$m."', '".$d."', NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL
);
") or die(mysql_error());
} else {
$load = file_get_contents("/proc/loadavg");
$load = explode( ' ', $load );
$total_load = $load[0] + $load[1] + $load[2];
$last_load = mysql_query("SELECT h".$h." FROM serverstats");
$daten = mysql_fetch_array($last_load);
$total = $daten["h".$h.""]+$total_load;
mysql_query("UPDATE serverstats SET h".$h."='".$total."'");
mysql_query("UPDATE serverstats SET ts ='".time()."'");
}
?>
And the database looks like this:
The Timestamp(ts) and the other values are the same, what did I make wrong?
You are missing where condition in update
UPDATE serverstats SET ts ='".time()."'"
While updating the database it should have where clause .. your sql should be something like\
UPDATE serverstats SET ts ='".time()."' Where = 'your condition'"
If you miss where in update clause .. then all records will get updated ..
NOTE please switch to mysqli_* or PDO . mysql_* are no longer officially maintained.
I assume, the ts is not an "on update CURRENT_TIMESTAMP" field. Having this would make it easier, leaving the ts stuff to the DB.
BUT:
these two statements
mysql_query("UPDATE serverstats SET h".$h."='".$total."'");
mysql_query("UPDATE serverstats SET ts ='".time()."'");
Will always update all records in the table - missing where clause.
Try this:
mysql_query("UPDATE serverstats SET h".$h."='".$total."', ts ='".time()."' ".
" WHERE year='".$y."' && month='".$m."'` && day='".$d."'");
and obviously
$last_load = mysql_query("SELECT h".$h." FROM serverstats " .
" WHERE year='".$y."' && month='".$m."'` && day='".$d."'");
since otherwise this is reading the entire table and your taking just an arbitrary first row.
It seems I don't have enough rep to leave comments but as an aside, you're using:
$h = date("H", time());
Which is in the form of 00 to 23 but the table columns are 01 - 24.
This may become an issue for example at 00:
$last_load = mysql_query("SELECT h".$h." FROM serverstats");
...

Categories