Page 1 of 1

Need a Guide to writing a SIMPLE "PHP/MySQL up/down voting"

Posted: Fri Jul 31, 2009 5:08 am
by Paradox187x
Before everyone starts pointing me to the HUNDREDS of scripts out there, let me make my case :)


I recently created a PHP/MySQL website database which shows something like a music chart list.

I'd like to enable visitors (none of this register crap, not interested in that yet) to vote up or down any number of tracks so that the chart is reorganised in order of popularity (so I'm happy for the page to reload).

But I can't find a SIMPLE guide to writing such a feature. They either include things like AJAX or others which (while include nice features) I'd rather leave FOR NOW or they don't contain one of the features below which I'm after.

1) IP Address checking (I'm aware of it's pitfalls, but I'd rather not the cookie method). I'm guessn' I'll need to create a second MySQL table, to-date I've only been dealing with ONE table.

2) Ability to vote UP or DOWN with the result shown as ONE NUMBER (do people suggest separate fields to store the UP & DOWNs and subtract one from the other when showing the result OR just keep them in the SAME database field.)

I found this one http://ad1987.blogspot.com/2009/02/redd ... l-and.html but it doesn't incorporate the IP Address checking.

I'm only 2weeks old in regards to website design & PHP coding, so this project is more of a learning experience then a need for all the bells & whistles. :dubious:

Thanx guys

Re: Need a Guide to writing a SIMPLE "PHP/MySQL up/down voting"

Posted: Fri Jul 31, 2009 11:39 am
by marty pain
When a voter casts a negative vote, a DELETE query should be run on the vote table.

Code: Select all

DELETE FROM vote WHERE i_id = 2 AND v_ip4 = '192.168.0.3' LIMIT 1
If you use this delete statement for a negative vote, it will only register a negative vote if that IP address had made a positive vote before, as it searches for the users IP in the table so it can be deleted. Or have a I missed something?

I would have another column in the votes table that stated if it was positive or negative, and when totalling subtract the negative from the positive.

Also, this might not work in practise as many internet users do not have static IP addresses, so they would be able to vote more than once. The only real safe method is to implement a users table so users would have to login before voting.

Re: Need a Guide to writing a SIMPLE "PHP/MySQL up/down voting"

Posted: Sat Aug 01, 2009 2:51 am
by Paradox187x
Thanx a heap guys,

McInfo, Your a legend.

I'll have to spend a bit of time digesting it (as I mentioned, I'm new to all this), But it's what I'm after.

Just to answer a few qestions
McInfo wrote:If you use this delete statement for a negative vote, it will only register a negative vote if that IP address had made a positive vote before, as it searches for the users IP in the table so it can be deleted. Or have a I missed something?
I'm more after a "positive OR negative" vote. I don't intend to allow them to recall their vote once they've made it. So they can choose if they like it or don't, that's all. For this reason I might have 2 database columns, one for positive & one for negative, where they can only add to either if their IP address doesn't exist in the database already. I suspect this method negates the need for a third column, (but you have provided an interesting solution to recalling/switching votes).
marty pain wrote:The only real safe method is to implement a users table so users would have to login before voting.
McInfo wrote:I was catering to Paradox187x's desire to have "none of this register crap". Without a login feature
It's a new site (no established visitors), based around a new idea (I hope), so I expect it will only attract a certain interest group. (SIDE NOTE: I don't expect voters from shared IP address locations, like companies, it's more of a home hobbie thing, so it should limit the number of visitors unable to vote.) Being new, my number one goal is to establish/retain a visitor base. I know as are with many people, requesting registration & logon to do simple tasks is a HUGE deterrent (it is when I visit sites, for quick info). I've noticed in articles that a "registered user" method is the only real secure one, over IP checking & Cookie work. BUT for this instance the voting is really only there to roughly gauge popularity of a record & therefore complete accuracy (to a degree) can be forfeited for ease of use.
Paradox187x wrote:this project is more of a learning experience then a need for all the bells & whistles.
If the site finds enough interest, without a doubt I'll plan to upgrade it & begin adding more security measures as I learn about them.

One last question, Is there a method of deleting an IP address from the database once it is over, say 24 hours old? I haven't found any easy way to do it.


Thanx both for your input. :D