How to update timestamp in a mysql database? - php

There is some error when I am executing this php code:
$today = date("Y-m-d H:i:s");
$sql = "UPDATE `deposit_admin_report` SET `READING`=$item,`Timestamp`=$today WHERE `METER_NUMBER`='NP-1353-'";
Error:
UPDATE `deposit_admin_report` SET `READING`=395,`Timestamp`=2015-11-27 09:08:33 WHERE `METER_NUMBER`='NP-1353-'
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '09:08:33 WHERE `METER_NUMBER`='NP-1353-'' at line 1
The column Timestamp has a type "timestamp" and default is "null" in the mysql table.
Will appreciate if anyone could help me out.
Thanks in advance

here is what you need:
$today = date("Y-m-d H:i:s");
$sql = "UPDATE `deposit_admin_report` SET `READING`=$item,`Timestamp`='".$today."' WHERE `METER_NUMBER`='NP-1353-'";
Also there is another way:
Simply use NOW()
Like this:
$sql = "UPDATE deposit_admin_report SET READING='$item',Timestamp=NOW()
WHERE METER_NUMBER='NP-1353-'";
That's it :)

use MySQL NOW() or change this $item to this '$item'
$sql = "UPDATE `deposit_admin_report` SET `READING`='$item',`Timestamp`=NOW()
WHERE `METER_NUMBER`='NP-1353-'";

$today = date("Y-m-d H:i:s");
$sql = "UPDATE `deposit_admin_report` SET `READING`= '" . $item . "',`Timestamp`= '" . $today . "' WHERE `METER_NUMBER`='NP-1353-'";
It sees the space in your date as a seperate word, and not as value of your timestamp.
Also, it is way easier to just use NOW()
$sql = "UPDATE `deposit_admin_report` SET `READING`= '" . $item . "',`Timestamp`= 'NOW()' WHERE `METER_NUMBER`='NP-1353-'";

Date field accepts a string which you should enclose in single quotes.
$sql = "UPDATE `deposit_admin_report` SET `READING`= $item, `Timestamp`= '$today' WHERE `METER_NUMBER`='NP-1353-'";

The question is already answered. Would like to bring into notice the caveat of using NOW() in Master / Slave architecture.
If timezone of slave server is different than that of master, the timestamp value stored on master will be different than the one on slave.
Its recommended to use following steps:
date_default_timezone_set('UTC'); // UTC is an example
$today = date("Y-m-d H:i:s");
$sql = "
UPDATE `deposit_admin_report`
SET `READING`=$item,`Timestamp`='".$today."'
WHERE `METER_NUMBER`='NP-1353-'
";

Related

Trying to UPDATE SQL DATE column via HTML form; Is this even possible? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 3 years ago.
When I run this Script the strtotime and date functions work, but when the SQL query runs the date column in the db remains blank.
$date = mysqli_real_escape_string($conn, $_POST['date']);
$day1 = strtotime($date);
$day1 = date('Y-m-d', $day1);
$id = 1;
echo $day2;
$sql = "UPDATE essay SET date = $day1 WHERE id = $id";
You have to add a quote over the $day1 like this way :
$sql = "UPDATE essay SET date = '$day1' WHERE id = '$id'";
Another way to do it by concatenate :
$sql = "UPDATE essay SET date = ".$day1." WHERE id = ".$id;
Unless an SQL field is an integer type or similar numeric type, data written to it should be quoted in an insert statement. In this case, your $day1 is something like "2019-04-18" so your SQL should read:
$sql = "UPDATE essay set date = '$day1' where id = $id";
The single quote should allow the query to succeed. Note that debugging this sort of thing is fairly easy, but isn't taught in some tutorials; if the query fails, try logging or echoing the MySQL(i) error:
$query = $db->query($sql);
if (!$query) echo $db->error;

MySQL multiple update sets in one query

How can I update multiple rows in MySQL using one query? It has been asked before, I know, but I have no idea how to use the CASE expressions.
I currently have this:
$sql = "UPDATE " . $DBtable . " SET clicks = clicks + 1 WHERE id= :id";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
How can I add a timestamp in the same query:
// Add timestamp
$date = new DateTime();
$now = $date->format('Y-m-d H:i:s');
"UPDATE " . $DBtable . " SET dl_time = ". $now . " WHERE id= :id";
dl_time is the name of the column.
You can set multiple columns in an update:
UPDATE " . $DBtable . "
SET clicks = clicks + 1,
dl_time = now()
WHERE id= :id;
Note that there is one set clause, with the assignments separated by commas.
Note that this version uses the database time rather than the application time. Unless you specifically want the application time, the database time makes the rows more consistent.

Can't update Date field in MySql using PHP

I have a DATE field in my table, and I'm trying to update it using the following code:
$query = mysqli_query($conn,$sql);
$todaydate = date("Y-m-d");
$sqlDate = date('Y-m-d', strtotime($todaydate));
$sql="UPDATE Library SET Loaned=1, LoanedDate=$sqlDate WHERE BookId=$bookId";
$query = mysqli_query($conn,$sql);
It updates the "Loaned" field fine, but always sets the Date field to "0000-00-00". Can anyone indicate what I'm doing wrong?
Youre missing ' quotes in your update query
Just use something like this
$sql="UPDATE Library SET Loaned=1, LoanedDate='$sqlDate' WHERE BookId='$bookId'";
$query = mysqli_query($conn,$sql);
You must have to add Quotes to the values... other wise the date may seem like an invalid integer to mysql. Here's how:
$query = mysqli_query($conn,$sql);
$todaydate = date("Y-m-d", time()); //<== DON'T FORGET THE 2ND ARGUMENT TO date(): TIME-STAMP. YOU MAY USE: time()
$sqlDate = date('Y-m-d', strtotime($todaydate));
$sql ="UPDATE Library SET Loaned=1, LoanedDate='{$sqlDate}' WHERE BookId='{$bookId}'";
$query = mysqli_query($conn,$sql);
Why don't you use only MySQL function to update date
Use this
$sql = "UPDATE Library SET Loaned = 1, LoanedDate = DATE(NOW())
WHERE BookId = '".$bookId."'";
Change your query with this code
$sql="UPDATE Library SET Loaned=1, LoanedDate= current_date() WHERE BookId='".$bookId."'";
Also check data structure in database.
This query is working fine:
$current_date = strtotime(date('Y-m-d H:i:s'));
$sql = "UPDATE user SET dt_added = '".$current_date."' WHERE id = '$id' ";
You're not concatenating the Date string properly in the SQL query.
Use ' and . operator to concatenate the string. Like this,
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$sqlDate."' WHERE BookId='".$bookId."'";
Altough it's good practice to use Prepared statement to pass arguments in SQL query.
Learn more about PHP Prepared Statements
Looking closer, I would just do:
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$todaydate."' WHERE BookId='".$bookId."'";
instead of
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$sqlDate."' WHERE BookId=$bookId";

Updating SQL using PHP - Error

Right so i have php code to update a SQL table. If i replace $_GET['emailID'] with a number say 1 the database IS updated. But otherwise no update. What seems to be wrong here
Table: emails
Fields: mailbox, emailID
$query = 'UPDATE `emails` SET `mailbox`=\'trash\' WHERE `emailID`='.(int)$_GET['emailID'];
Do like this
$query = "UPDATE `emails` SET `mailbox`='trash' WHERE `emailID`=".intval($_GET['emailID']);
Can you try this,
$query = 'UPDATE `emails` SET `mailbox`=\'trash\' WHERE `emailID`="'.(int)$_GET['emailID'].'" ';
Value of attribut must be selected by single quotes. Try this:
$query = "UPDATE `emails` SET `mailbox` = 'trash' WHERE `emailID` = '" . intval($_GET['emailID']) . "'";
$query = "UPDATE `emails` SET `mailbox`='trash' WHERE `emailID`= ".$_GET['emailID'];
Try this one sure it will work

Calling NOW() function in MySQL

I'm using php for making queries for mysql. Here is one:
UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = "$resEmail"
curDate - DateTime type. The problem is that after this query curDate of given email is
0000-00-00 00:00:00
What's wrong?
Your PHP probably looks like this now:
$sql = 'UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = "$resEmail"';
The single quotes prevent the variable's value from being substituted into the string. You will need to change it to this:
$sql = "UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = '$resEmail'";
You should also be aware that you may have an SQL injection vulnerability here. Consider using mysql_real_escape_string to escape the email address.
$sql = "UPDATE `subscribers` SET `curDate` = NOW() WHERE `e_mail` = '" .
mysql_real_escape_string($resEmail) . "'";
Since you're using timedate function which is based on proper timestamp, try setting it with Timestamp(Now()).

Categories