How to Clear an ip from a table every 24 hours

Ok so I am making a “voting” page… And I need the table to clear the an ip 24 hours after the ip is added onto the table.
Here is my Table

[code]CREATE TABLE tempvotes (
id int(5) NOT NULL,
time int(9) NOT NULL,
ipaddress varchar(15) NOT NULL,
voted int(1) NOT NULL default ‘0’
) ENGINE=MyISAM;

CREATE TABLE votes (
id int(11) NOT NULL auto_increment,
time int(9) NOT NULL,
ipaddress varchar(15) NOT NULL,
authcode varchar(6) NOT NULL,
used int(1) NOT NULL default ‘0’,
username varchar(14) NOT NULL,
guid varchar(45) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM[/code]

Add a field after the ipaddress called something like ipdate and store the date and hour in it.
(Hour should the 24-hour notation version.)
Next, whenever anyone votes, just do a quick query and look for any vote where the current time and saved time is larger that 24. The math is a little tricky as you need to do as you need to check for #days first, etc… But, it is doable… If the query returns anything, them drop those records…

Post back with any code questions if you get it working or partly working…

I’m going to be honest, I am actually trying to learn php… And this is just a problem i encountered… If theres any chance you could step by step do it/help me do it. That would be amazing.

Okay, let’s see… I notice you already have the “time” field in the tables. Which table is which?
You have one called votes and one tempvotes??? Also, do you have experience reading info from the database and storing things in it? Do you already have the voting site up and working and just help clearing out the old votes? More info please…

In the meantime, here is the general layout of just the checking for timed-out votes.

  1. connect to the database.
  2. connect to the table in the database.
  3. setup the SQL query to search for the expired votes.
  4. delete all votes that are expired.
  5. close connection to the database.

Okay, now some ideas on the timebase you should use. First, you said 24 hours max. So, I would use a 24-hour format. When the vote is created, get the date and the 24-hour time (1 to 24) and store it in the database time field. You can store it as year.month.day.time, all digits so today 3/8/2012 at 9:28 would be something like 2012030802128. (year first grantees no leading zeros.) Actually, what I would do is add 24 hours to this date/time combo. And, call it the EXPIRATION-DATE. When setting up the SQL query in #3 above, it would be something like this:
$current_time=*** Here create the date format above for today’s time. More on that in another post!
$query="SELECT * from votes WHERE (time < " . $current_time . “)”;
This query will pull all records form the votes where the expiration date/time is less than the current date/time combo. These would all be 24 hours or more old and would then all be deleted. (#4) To do that, you would use a while loop to go thru all of the records and delete them.

*** Well, basically that would be the way I would do it. Hope these ideas help…

Ok well I understand what your saying 100%, which is good… But I have no idea how to actually write the tables… Just add the sql/import the sql.
I just don’t really know how I would do it.

Well, learning PHP, HTML, SQL, and combining them all together could be a long project.

Let’s see, first Google is you buddy, Google is your friend… Learn how to search for things and you will succeed. When stuck, post your question here. When posting here, keep the details and question simple. So, if you have a little code that you are stuck on, post it and show us the error.

Let’s start with SQL and databases. Making them into PHP is fairly easy. But, since you only understand SQL, you have to start from scratch… This is what you will need to learn.

  1. HTML - You need to create a form for your voting page. This will include all of your info you want the user to input and some options, such as what they are voting for. And, how to make it “pretty” and neat looking.

  2. PHP - The form on the html page will “post” to the PHP file which will handle all the work and display the results. You need to learn how to connect to the database, read from the database and write to the database. This is easy to do with SQL queries.

So, use your buddy Google and search for things like “PHP database connection sample code”, etc…
Search for each part of your project and create a project plan. Also, search this site for answers, too. Other people have asked most of these questions before! Then, when stuck, ask us questions…

That is all I can do to get you started… Good luck!

Thanks you’ve been such a great help, but is there any chance you could spoon feed me with what to do… I have the page up already…

Well, first, have you got that database already in place?

Do you have the page you said you have in place already reading from database?

Do you have the voting page posting to the database yet?

We can help you here, but, what step by step are you needing. (If you already have the page in place, which page and what part is missing…) Also, a little bit of your code would be nice…

Yes, I have everything up and working…
Just I need a code to delete the ip after 24 hours of being their.

Okay, I can help tonight with this…

First, walk me thru in a few lines of what happens when someone votes. I will assume they log-in.
Then, vote. You store their vote. Then, later on the votes are used for something…

So, you want step by step code for checking if an already databased vote is 24 hours old.
We can do that. Tell me where you are saving the date now and it’s format. If you do not already save it, tell me so. I can come up with more ideas for you…

(Leaving for an hour or two, will check in later with you!)

Sponsor our Newsletter | Privacy Policy | Terms of Service