Jump to content

$session problem with remove data function


Recommended Posts

$query = "DELETE FROM users WHERE logout_datetime < (DATE(NOW()) - INTERVAL 3 DAY)"; //No user_id $stmt = $pdo->query($query)

So this is what I'm going to need?

stmt = $dbc->prepare("UPDATE users SET logout_datetime = NOW() WHERE user_id = ? "); // prepare query with placeholder (?) for id value $stmt->bind_param('i', $_SESSION['user_id']); $stmt->execute()

What is this then, the new code, what I posted above is rubbish.

4 hours ago, tryingphp said:

$query = "DELETE FROM users WHERE logout_datetime < (DATE(NOW()) - INTERVAL 3 DAY)"; //No user_id $stmt = $pdo->query($query)

So this is what I'm going to need?

stmt = $dbc->prepare("UPDATE users SET logout_datetime = NOW() WHERE user_id = ? "); // prepare query with placeholder (?) for id value $stmt->bind_param('i', $_SESSION['user_id']); $stmt->execute()

What is this then, the new code, what I posted above is rubbish.

I'm trying really hard now not to start using foul language. You are doing exactly what I said before. You aren't reading what is posted - as in looking at it critically and trying to comprehend what is being provided. Instead you seem to be fixated on some erroneous logic and are trying to fit the responses into that. Do you understand that what you want to accomplish has two separate processes (as I've explained multiple times)? If you have a question about what I posted ask a specific question about it instead of coming up with some nonsense question.

On 11/18/2019 at 9:31 PM, tryingphp said:

Well, another short fuse nutter I am dealing with.

TRUST ME, If I could do this I would of. I am terrible at all of this. 🤬

Short fuse? I've taken the time to respond multiple times providing the same information which you ignore because you've got some preconceived notions in your head that you can't get rid of. Think of it this way, someone is taking time out of their day to try and help someone for no reason other than goodwill - there is no compensation for their time. Yet, the person they are trying to help ignores/disregards the help being provided such that the person providing the help invests even more time and energy helping that person. So, who is the "nutter" in this scenario? The person that is attempting to help or the person that won't take the time to try and understand the help being provided?

You are "terrible" at this because you are not taking the time to understand the process. Instead you seem to be throwing things at the wall to see what will stick. Let's try one more time:

Your requirement is to delete a record three days after the user performs some event (which I assume is a logout). Is this correct?

1) You first need to perform an action when the user selects the option to logout. As stated many times, you would want to set a datetime/timestamp value for the record. STOP HERE. Is there something about that statement that you do not 100% understand? Do you understand why the record would need to be updated and why we would use a datetime/timestamp value for that update? If you are not 100% clear, ask a question about that.

You've previously provided info that when this action takes place the user_id is available in the session data. So, we would accomplish the above action like so:

//Create a prepared statement with placeholder for the user_id of the record to be updated
$stmt = $dbc->prepare("UPDATE users SET logout_datetime = NOW() WHERE user_id = ? "); 
//Bind the user_id value to the prepared query and execute it
$stmt->bind_param('i', $_SESSION['user_id']);
$stmt->execute();

STOP HERE. Do you understand all of the code above? If not, ask a question about what you don't understand or look at the manual for any function you don't understand.

2) Now that users will have a datetime/timestamp value set when they log out, there needs to be a separate process that will delete the records after three days. To accomplish this, you would create a separate script which will need to be executed on a regular basis (e.g. daily). You will need to check your host options o how to do this. Typically these will be called CRON jobs. You basically set up a scheduled task to run a script at a predetermined interval. STOP HERE. Do you understand what was just stated? If not ask a specific question. If you don't know how to create a CRON job, you need to go into your hosting options, check help and/or contact support.

Once you know how to create a CRON job, you just need to create the script that will run every day (or whatever time interval you choose). Since we have a datetime/timestamp of when users have logged out, a single query can be executed to delete all records where that value is > 3 days (as has been provided previously)

$query = "DELETE FROM users WHERE logout_datetime < (DATE(NOW()) - INTERVAL 3 DAY)"; //No user_id
$stmt = $pdo->query($query)

STOP HERE. Do you understand what that query is doing and why user_id is not needed?

This is my last attempt. I hope you will READ the above information and attempt to comprehend it. If you have any questions that show that you have put some energy into trying to trying to figure this out instead of just haphazardly writing new code, I will try to answer. Otherwise, I which you all the best in your endeavors.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.