简体   繁体   中英

Delete an entry from db on PHP page exit

I have a php page in which a user tries to find online people.

There a search button, clicking on which, an entry is made for the current user in the database and the control goes inside a loop, where every 5 secs a search is made in the database to find if a new entry has been made, if an entry is found then the details of the partner is shown to him.

I want that if the user exits or navigates away from the page before a partner is being found, then his entry must be deleted from the db.

I am trying to store the 'id' created against the user inside a session variable, make an ajax call and delete the entry, but somehow this concept is not working.The data is not getting deleted. Is this because of the loop which is still finding the user or something else, m not able to get it.

Can anyone tell me what is going wrong with my approach ?

A code snippet that I am using is hereby

window.onbeforeunload = function() {
 funcDeleteonexit();
return "Are you sure you want to navigate away?";
}

function funcDeleteonexit(){
  $.get("functions.php",{
      data:"delete"
  },function(data,status){
 });
 }

Inside my functions.php, I have

if($_GET){
if ($_GET['data']=="delete"){
    echo deletefromDb();
   }
}


function deletefromDb() {

    $mysqli = new mysqli("localhost", "root", "", "test");

    /* check connection */
    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $currentid = (int)$_SESSION['currentId'];

    $query1 = "delete from test where id =". $currentid;

    $mysqli->query($query1);
    $mysqli->close();
    return $currentid;
}

If you want something done when someone exits the page, this will in most cases give you trouble if you want a specific event to be fired then. There will be cases a browser cannot fire the event (pe client crash etc.).

Therefore I'd suggest a different approach. Control this from the server side, and don't rely on the users browser or input.

Firstly, you can poll your data while regularly firing Ajax events from the browser. You've not stated this, but I think you do something like this:

function yourrequest(){
   $.ajax({
       type: "GET", 
       url: "your/php/script.php",
       success: function(data){
          // do something, p.e. update a div 
       }
   });
};

setInterval(yourrequest, 5000);

(Please don't tear this code sample apart, its just to show the basics).

On the server side, you already have the users id somewhere in your database. Add a timestamp field to this table (call it last_request or something like that). Everytime you send some data to the client, check if there are users with a last_request below your desired threshold, and delete those ids. No need to wait for a specific event then.

This needn't be done exactly there, you can also do something different, pe a cleanup job cronned every 5 minutes or so which does this seperately to not disturb the users request at all.

I'm suggesting that browser just not sending ajax, or interupting it. So try to make your AJAX request synchronous. For JQuery seems to be set async parameter to false

$.get("functions.php",{
      data:"delete",
      async : false
  },function(data,status){
 });

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