Home > front end >  How to implement each time to close the page to delete this insert records in the database
How to implement each time to close the page to delete this insert records in the database

Time:05-08

I am doing a website, every time a user to enter a sessionid is generated, users make some choice, the choice is included in the database, I now want to when the user closes the page to delete the record of the user in the database, use the onbeforeunload function, but found that did not perform when close the page, but when refresh again, or enter the page to perform the function in the content, simply close the page does not perform operations, want to ask how close the page can be deleted in the database content

The following part of the code
 & lt; script> 
Window. Onbeforeunload=function (e) {
SetTimeout (function () {
SetTimeout (beforeloadResult, 50)
}, 50);
Return 'sure? ';

}

The function beforeloadResult () {
<? PHP
Set_time_limit (0);
The ignore_user_abort (true);
$servername="localhost";
$username="root";
$password="wgz52157";
$dbname="myFirstDB";
//create a connection
$conn=new mysqli ($servername, $username, $password, $dbname);
Mysqli_query ($conn, "DELETE FROM MyUsers WHERE id='$sessionid'");
mysqli_close($conn);
? & gt;;
Window. The location. Reload ();
}
</script>
  • Related