Jump to content

Archived

This topic is now archived and is closed to further replies.

vigiw

PHP Form Auto-Empty Script

Recommended Posts

Hello,

I have a working PHP form that sends data to my MySQL database.  It is used for a weather alert system.  One of the fields in the form & database is an expiration field.  It shows the user when the alert will expire.  Is there a way to auto-delete just the data sent with that form when it is supposed to expire?  I have many weather alerts and I would like them to be emptied from the database when it is needed.

Please help, this is a much needed feature.  All help appreciated :)

Thank you so much!

Share this post


Link to post
Share on other sites
Periodically run a query such as

DELETE FROM alerts WHERE expiretime < NOW()

Share this post


Link to post
Share on other sites
Ok.. I have many tables in this database that contain this data that need to be emptied.  Do I have to name each table in there or the database name, or what?

Thanks. ;D

Share this post


Link to post
Share on other sites
Sounds like a case for re-thinking db design. You shouldn't be creating and dropping tables from an application. I'm surprised your DBA allows it.

Can you explain your whole scenario, from creating the alerts (and why in different tables) to deleting them?

Share this post


Link to post
Share on other sites
No, I don't want to delete tables.  I was just verifying that it only empties the data that I want it to, and not deleting.

I have a weather forecast system, it officially launched to the public last night.  I have different forecast areas and different alert types and so they are in their own tables of one database.  It is already working, the hard part is over.  I just want the old data to be removed.

I have another question:

What format does the expiretime have to be in?

Date, Time, maybe, I don't know the exact format to make the database understand.

Thanks.

Share this post


Link to post
Share on other sites
Make it a DATETIME field. Write to it in the format YYYY-MM-DD HH:ii:ss.

I'd set it up so that when you add an alert to one of your tables, also call the query to delete any expired ones from that table.

Share this post


Link to post
Share on other sites
Ok Thanks..


Is there a way to make this script automatic?  Maybe cron jobs?

I honestly do not know what a cron job is and have never used one so I would not know what to do.

It seems very tedious running this every so often since there will be quite the amount of alerts to empty when needed.

Share this post


Link to post
Share on other sites
[quote]Is there a way to make this script automatic?[/quote]

[quote author=Barand link=topic=109197.msg440039#msg440039 date=1159038196]
I'd set it up so that when you add an alert to one of your tables, also call the query to delete any expired ones from that table.
[/quote]

Share this post


Link to post
Share on other sites
Ok thank you and sorry for the late reply here, but how do I make the following script:

DELETE FROM alerts WHERE expiretime < NOW()

use multiple tables such as:

alerts
alerts2
alerts3

Thanks!

Share this post


Link to post
Share on other sites
You said you have working form that writes the alerts to your database.

So if you processing script is inserting a record into, say, alerts2 table, then also

mysql_query ("DELETE FROM alerts2 WHERE expiretime < NOW()");

Share this post


Link to post
Share on other sites

×

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.