Php/JavaScript refresh page if MySQL data is changed-ThrowExceptions

Exception or error:

I’m working with php and mysql and I want the website to refresh whenever it detects a mysql change.

I’ve tried the following and it didn’t refresh.

Main.php

require_once "include/config.php";
$var = $_GET['store'];
$var2 = $_GET['location'];
$sql = "SELECT * FROM devices WHERE var = '" . $var . "' AND var2 = '" . $var2 . "'";
$result = mysqli_query($conn,$sql);
$row1 = mysqli_fetch_assoc($result);
$name = $row1["var3"];
?>
//Other HTML Stuff here
<script type="text/javascript">
var currName = "<?php echo $name; ?>"; 
var processUpdate = function( response ) 
{
var x=response;
if ( currName != x ) 
{
    currName = response;
    window.location.reload();       
}
}

var checkUpdates = function()
{
serverPoll = setInterval( function()
{
    $.get('include/check-update.php?var=<?php echo $var; ?>&var2=<?php echo $var2; ?>', 
        { 
        lastupdate: 1 
        }, processUpdate, 'html');}, 5000 ) };
$( document ).ready( checkUpdates );
</script>

And here is check-update.php which is located in a subfolder called ‘include’

$var = $_GET['var'];
$var2 = $_GET['var2'];
$sql = "SELECT * FROM devices WHERE var = '" . $var . "' AND var2 = '" . $var2 . "'";
$result = mysqli_query($conn,$sql);
$row1 = mysqli_fetch_assoc($result);
$name = $row1["var3"];
echo $name;

mysqli_close($conn);

Also note that inspect element it does show the correct info https://prnt.sc/rf0igg

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *