简体   繁体   中英

How to check if there is an added record on the database

I have here my index.php. It already contains the layout and the php code inside it. I just want to know if there is a new record on the database. From my research it needs to be refreshed. I tried the javascript auto refresh and the whole page now refreshes so if there will be new records the table will automatically show the new record. But now my problem is how to know if there are changes. I am able to show if there is an update on the database through refresh but I am not able to notify if there are changes. I just want to put notification if there are new records. So for now I want to know how to check if there are new records on the database. Here are my code.

<div class="panel panel-default">
    <div class ="panel-body">


    <!-- TABLE OF REPORTS -->
    <div align = "center" style = " font-size: 9000px">
        <table class="table table-striped" name = "report">
            <thead>
                <tr align = "center"class = "active" >

                    <td class  = "theads" >Id</td>
                    <td class  = "theads" >Message</td>
                </tr>
            </thead>

            <!--Start of php code -->
            <?php   
            $page=1;//Default page
            $limit=10;//Records per page
            $start=0;//starts displaying records from 0
            if(isset($_GET['page']) && $_GET['page']!='')
            {
                $page=$_GET['page'];
            }
                $start=($page-1)*$limit;

                //Get total records (you can also use MySQL COUNT function to count records)
                $query=mysql_query("select report_id from reports");
                $rows=mysql_num_rows($query);

                $query=mysql_query("select * from reports order by datetime DESC LIMIT $start, $limit");


                if(mysql_num_rows($query)>0)
                {
                    while($row=mysql_fetch_array($query,1))
                    {
            ?>

            <tbody>
                <tr class = 'active'>
                <td align = 'center'><?php echo $row['report_id'] . "<br>";?></td>
                <td align = 'center'><?php echo $row['message'] . "<br>";?></td>
                </tr>
            </tbody>

                <?php
                    }
                }
            ?>
        </table>

    </div>

    </div>
</div>

If you want to do this notification system on client side then you can create a cookie with the latest created date and check if any created date is greater then date of cookie then count the rows which having greater date then cookie's value.

If you want to do it on Server side then you have to save last seen created date some where in database table and do above process.

$query=mysql_query("select * from reports order by datetime DESC LIMIT $start, $limit");
if(isset($_SESSION['chk']))
{
    $sql=mysql_query("select count(*) as new_record from reports where datetime>$_SESSION['chk']");
    $count=mysql_fetch_array($sql);
    $new_record=$count['new_record']; // new records
}

if(mysql_num_rows($query)>0){$i=0;
while($row=mysql_fetch_array($query,1)){if($i==0){$_SESSION['chk']=$row['datetime'];$i++;}
?>

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