TMNuclear
TMNuclear

Reputation: 1175

Automatically delete record when past current timestamp or date

I'm coding a password recovery script. When the user want's to recover his password, a 50 string of random validation code is being written into the database, which will be used as $_GET to validate.

This table called recovery is var(50) and unique. After accepting the link, this field will be erased. But what if the user is not going to click the link? This has to be deleted from the database, because the chance that this random 50 string will be created gets bigger and the database gets unnessessary data.

I've been looking around and saw this:

INSERT INTO what_ever ('', '', now(), DATE_ADD(NOW(), INTERVAL 3 DAY));

This way I enter an expiration date, but how can MYSQL delete this himself when the time is right? Is that even possible?

If not, what's my best option here?

EDIT:

Of course 3 days is too much. I was thinking about 15 minutes.

Upvotes: 1

Views: 863

Answers (2)

Vince
Vince

Reputation: 3334

You can write a trigger that deletes old data when inserting new rows.

If you don't know how to write a trigger, use an advanced MySQL client like HeidiSQL, it will help you with some GUI.

Upvotes: 3

Kickstart
Kickstart

Reputation: 21513

You could put a trigger on insert to the database to delete any records older than a certain amount.

Upvotes: 2

Related Questions