Php 如果数据库中的表不断更改,我应该如何更新

Php 如果数据库中的表不断更改,我应该如何更新,php,jquery,html,mysql,Php,Jquery,Html,Mysql,我有一个任务,我将解释我的工作应该是什么样的 首先,我使用$.get方法从数据库中的表中获取行的总数,并向html文件显示值 在1小时或2小时或2天或更长时间后,表中的行总数将发生变化 除了刷新页面或重新加载页面之外,我应该如何更新html文件中的总行数 我的代码如下: calculatetotalattack.php <?php include 'config.php'; $con = mysqli_connect ($dbhost, $dbusername, $dbpas

我有一个任务,我将解释我的工作应该是什么样的

首先,我使用
$.get
方法从数据库中的表中获取行的总数,并向html文件显示值

1小时2小时2天或更长时间后,表中的行总数将发生变化

除了刷新页面或重新加载页面之外,我应该如何更新html文件中的总行数

我的代码如下:

calculatetotalattack.php

<?php

   include 'config.php';
   $con = mysqli_connect ($dbhost, $dbusername, $dbpassword) or die ('Error in connecting: ' . mysqli_error($con));

   //Select the particular database and link to the connection
   $db_selected = mysqli_select_db($con, $dbname ) or die('Select dbase error '. mysqli_error());
   //Make A SQL Query and link to the connection
   $totalattackview = mysqli_query($con, 'SELECT * FROM attackview'); // get number of row from attackview

   //Calculate the total number of rows from the table
   $totalofrowsattack = mysqli_num_rows($totalattackview);

   echo $totalofrowsattack;

   mysqli_close($con);

?>

HTML

<html>
<head>
<title>Updating the Total Number of the total rows</title>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<body>
<div id="totalattack"></div>
<script type="text/javascript">
function totalAttacks(val) {
        $('#totalattack').html(val);
    }

    $.get({
        url: 'calculateTotalAttack.php',
        dataType: 'text',
        success: totalAttacks
    });
</script>
</body>
</html>

正在更新总行的总数
函数totalAttacks(val){
$('#totalattack').html(val);
}
美元({
url:'calculateTotalAttack.php',
数据类型:“文本”,
成功:全面攻击
});

有什么方法吗?

U可以使用一个时间间隔来每次调用totalAttacks()


以下是文档:

您可以在html文件中动态地向表中添加行或列,然后显示数据

<!DOCTYPE html>
    <html>
    <head>
    <style>
    table, td {
        border: 1px solid black;
    }
    </style>
    </head>
    <body>

    <p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

    <table id="myTable">
      <tr>
        <td>Row1 cell1</td>
        <td>Row1 cell2</td>
      </tr>
      <tr>
        <td>Row2 cell1</td>
        <td>Row2 cell2</td>
      </tr>
      <tr>
        <td>Row3 cell1</td>
        <td>Row3 cell2</td>
      </tr>
    </table>
    <br>

    <button onclick="myFunction()">Try it</button>

    <script>
    function myFunction() {
        var table = document.getElementById("myTable");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = "NEW CELL1";
        cell2.innerHTML = "NEW CELL2";
    }
    </script>

    </body>
    </html>

表,td{
边框:1px纯黑;
}
单击按钮在表的第一个位置添加新行,然后添加单元格和内容

第1行第1单元 第1行第2单元 第2行第1单元 第2行第2单元 第3行第1单元 第3行第2单元
试试看 函数myFunction(){ var table=document.getElementById(“myTable”); var行=table.insertRow(0); var cell1=行插入单元格(0); var cell2=行插入单元格(1); cell1.innerHTML=“新建cell1”; cell2.innerHTML=“新建cell2”; }
insertRow()方法创建一个空元素并将其添加到 桌子

使用setInterval()/setTimeout()方法连续调用函数以使其成为动态的


另外,由于您不知道行/列何时会增加,因此可以使用setInterval()不断监视表中行数的任何更改,如果有任何更改,则使表增长

您可以按计时器运行$.get函数,如下所示:


正在更新总行的总数
函数totalAttacks(val){
$('#totalattack').html(val);
}
$(文档).ready(函数(){
var timer=setInterval(函数(){
美元({
url:'calculateTotalAttack.php',
数据类型:“文本”,
成功:全面攻击
});
log(“请求更改”);
}, 5000);       
});

您可以使用ajax和jquery动态上传数据,就像

$document.ready(){

$.ajax({
        type: "POST",
        url: "",
        data: {
            first_name: $("#namec").val(),
            last_name: $("#surnamec").val(),
            email: $("#emailc").val(),
            mobile: $("#numberc").val(),
            password: $("#passwordc").val()
        },
        success: function(response) {
            //dynamically add your data from here like make the new data equal to old one

        },
        error: function(response) {
            console.log(response);
        }
    });
}

$.get
获取行数或setInterval/timeout获取行数。在这种情况下,您需要动态地将行添加到表中。@Script47,除了$.get部分,我不理解您的句子。。