简体   繁体   中英

MySQL PHP Updating DateTime only when specific column is updated

I've made a very simple PHP script that increments 1 to my dishwasher_count row in my members table. It works as it's supposed to when it looks like this:

PHP:

include "admin/vars.php";

//connect to the database
$db = mysql_connect($db_host,$db_user,$db_pass);
      mysql_select_db($db_name);


//Runs when user sends GET
if(isset($_GET['name'])) {
$name = $_GET['name'];
// Increment the dishwasher_count by 1 and update the dishwasher_latest field value of the requested name.
$update = mysql_query("UPDATE members SET dishwasher_count = dishwasher_count + 1 WHERE name='$name';

}


$result = mysql_query("SELECT name, dishwasher_count, dishwasher_latest FROM members ORDER BY dishwasher_count DESC");
while($row = mysql_fetch_assoc($result)){
      echo "<h2>" . $row["name"]. 
      " <a id='1' href='./?name=" . $row["name"] . "' class='fa fa-plus-circle' ></a></h2>
      <p class='margin'>Urplockningar: " 
      . $row["dishwasher_count"]. "<br>
      Senaste: " . $row["dishwasher_latest"] . "</p>";
}

mysql_close($db);

The problem is that i want it to also show when the latest increment was, I got it working by using "TIMESTAMP" and "on update CURRENT_TIMESTAMP" in MySQL. But the problem is that it will update the TIMESTAMP when any rows are updated, and I only want it to update the dishwasher_latest to the current time when the dishwasher_count updates.

I've tried doing this, but it seems to brake the SQL:

$update = mysql_query("UPDATE members SET dishwasher_count = dishwasher_count + 1 WHERE name='$name'; UPDATE members SET dishwasher_latest=now()");

I've been working with this problem for a while now, but can't get it to work. How would I get this to work?

If you want to record when a specific column was changed, then you cannot use timestamp column, that is for recording when any fields within the record changes.

Your idea to update a datetime field to current time is a good one, just you need to work on the implementation. mysql_query() does not support executing multiple statements in 1 call. You should not be even using mysql_*() functions at all, they are deprecated.

Solutions:

  1. Combine the 2 updates in a single query: update table set field1=..., field2=... where field3=...
  2. Use mysqli or pdo insted of mysql module and execute the 2 statements with them. Just make sure that the 2nd one aldo has a where criterion, otherwise the whole table will be updated.
  3. Have 2 separate mysql_query() calls with the 2 statements. Again, watch out for the where.
  4. You could use a trigger as well, but that would be an overkill.

Are you looking for something like this?

UPDATE members SET dishwasher_count = dishwasher_count + 1,dishwasher_latest=now() WHERE name="$name";

This will only update the row affected with a new timestamp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM