简体   繁体   English

更新数据库中的数据时刷新PHP页面

[英]Refresh PHP Page when data in Database Updated

I want my client side web to auto-refresh when data in my Database updated, when adding or deleting data I have succeeded however when the data changed, it still fails. 我希望客户端网络在数据库中的数据更新,添加或删除数据成功时自动刷新,但是在数据更改时仍然失败。

This is my code for checking data from database: 这是我用于检查数据库中数据的代码:

<script> 
var row1 = "<?php echo $variable; ?>";

var processUpdate = function( response ) {
    var x = response;
    //console.log(x);
    if (row1 != x) {
        window.location.reload();
    }
}

var checkUpdates = function() {
    serverPoll = setInterval(function() {
        $.get('check.php', { lastupdate: 1 }, processUpdate, 'html');
    }, 1000)
};

$(document).ready(checkUpdates);

</script>

check.php : check.php

$query = mysqli_query($koneksi, "SELECT * FROM table");
$number = mysqli_num_rows($query);
echo $number;

What should I change to be automatically refreshed if every data in the table is changed? 如果表中的每个数据都被更改,我应该更改为自动刷新吗?

You can use trigger that will insert some info about each table update in another table, and then just query the num rows on 'changes' table in a similar way you check for new ones here: 您可以使用触发器,该触发器将在另一个表中插入有关每个表更新的一些信息,然后以与您在此处检查新表类似的方式查询“ changes”表上的num行:

DELIMITER //
CREATE TRIGGER table_update_trigger AFTER UPDATE ON table
  FOR EACH ROW BEGIN
    INSERT INTO table_history
    (
      change
    )

    (
      NEW.some_value,

    );
  END
//

The advantage of this solution is you don't need to introduce/rely on/maintain any other db system like Redis and the checking code is not responsible for keeping and updating any counters and queries for updates, inserts and deletes in a similar fashion. 此解决方案的优点是您不需要引入/依赖/维护任何其他数据库系统(例如Redis),并且检查代码不负责以类似方式保留和更新任何计数器以及对更新,插入和删除的查询。 Also you might extend the table_history table to log all the fields you are interested in in terms of tracking changes and end up having useful changelog for the purpose of the application. 另外,您可以扩展table_history表以记录所有您感兴趣的字段,以跟踪更改,并最终获得有用的更改日志以用于应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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