MySQL使用PHP将DATETIME更新为NOW()

MySQL使用PHP将DATETIME更新为NOW()

问题描述:

This all worked before I added the section to update "last_update".

if((time() - $last_update) > 7200){
$sql = $dbh->prepare("UPDATE item_list SET quantity=:quantity, price=:price, last_update=:now WHERE item_name=:itemname");
                $sql->bindParam(':quantity', $json->volume);
                $sql->bindParam(':price', $json->lowest_price);
                $sql->bindParam(':itemname', $row['Item_Name']);
                $sql->bindParam(':now', "NOW()");  //This doesn't work
                $sql->execute();
}

When this is called I want to make last_update the date and time now. In the database it is currently a DATETIME, and when I last_update I origianly set them to NOW();

Doing this I get the error Fatal error: Cannot pass parameter 2 by reference in.... Directory

I know it expects a variable, I'm not sure how to fix it though. I tried setting

$now = "NOW()"; $sql->bindParam(':now', $now);

No prevail. Any help?

在添加更新“last_update”的部分之前,这一切都有效。 p>

  if((time() -  $ last_update)> 7200){
 $ sql = $ dbh-> prepare(“UPDATE item_list SET quantity =:quantity,  price =:price,last_update =:now WHERE item_name =:itemname“); 
 $ sql-> bindParam(':quantity',$ json-> volume); 
 $ sql-> bindParam(':  price',$ json-> lowest_price); 
 $ sql-> bindParam(':itemname',$ row ['Item_Name']); 
 $ sql-> bindParam(':now',“NOW  ()“);  //这不起作用
 $ sql-> execute(); 
} 
  code>  pre> 
 
 

当调用它时我想让last_update成为日期和 现在的时间。 在数据库中它当前是DATETIME,当我last_update时,我原始地将它们设置为NOW(); p>

这样做我得到错误致命错误:无法通过参数2 引用....目录 strong> p>

我知道它需要一个变量,我不知道如何修复它。 我尝试设置 p>

$ now =“NOW()”; $ sql-> bindParam(':now',$ now); code> p>

不存在。 有什么帮助吗? p> div>

Why you need to bind, just put NOW() directly

$sql = $dbh->prepare("UPDATE item_list SET quantity=:quantity, price=:price, last_update=now() WHERE item_name=:itemname");

If your last_update column is looking for a UNIX timestamp, then do :

$now = time(); 
$sql->bindParam(':now', $now);

If it's after a different time format, use date(), and the relevant formatting it has to set the date and time

You can keep your bind query as it is & remove the last_update column from the query.

Since you are updating other things in the record via another query, then you can set the default value of the field last_update to CURRENT_TIMESTAMP & set it's attribute as ON UPDATE CURRENT_TIMESTAMP. That will ensure it automatically updates itself with the current time now() whenever that record is updated.

It wouldn't be the best thing to remove bind() as you rightly said to prevent SQL injection attempts.