I am trying to develop a small webpage which just has to generate random number and post that generated number to MySQL database. it's a simple script which I got from various user's answer and help over the internet. thanks to them . everything is working right but I have a small issue while executing script, when I open that webpage, it generate number and post to Database, but when I close webpage , it stop to generate number, I tried searching everywhere but didn't get actual solution that I want. following are code I am using . Thanks for your help
<?php
include_once 'database.php';
if ($conn) {
    echo "success<br>";
}
$digits = 3;
$result = rand(pow(10, $digits-1), pow(10, $digits)-1);
//this is sum
$num = $result;  
$sum=0;
$rem=0;  
  for ($i =0; $i<=strlen($num);$i++)  
 {  
    $rem=$num%10;  
    $sum = $sum + $rem;  
    $num=$num/10;  
    $total = substr($sum, -1);
     
 }  
 
 //echo "Sum of digits $result is $sum"; 
$finres = $result.'-'.$total;
// $result = mysql_real_escape_string($result);
// $total = mysql_real_escape_string($total);
// while( $digits = 3 )
// {
//     echo $result;
//    sleep(10);
// }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="refresh" content="5" > 
    <title>Ghanta Bazar</title>
</head>
<body>
<p><?php echo $finres; ?></p>
<?php 
$sql = "INSERT INTO ghanta_result (result)
VALUES ($finres)";
if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
    
</body>
</html> 
     
    