Click to See Complete Forum and Search --> : PHP and MySQL star rater system on HTML pages


gameringer.com
06-14-2009, 01:19 PM
hello everyone,

I have been looking for a solution to this problem for the LONGEST time

so what i want is a 5 star rating system on my html pages that stores its information in a mysql database.

I have a free online games website (www.gameringer.com), so i need to have it so i put in one code (the same code) into all the pages and it automatically makes its own table, for its own url, in the mysql database.

If you have a solution or comments, or just a suggestion, please answer

Thanks,
GameRinger.com

Inferno_str1ke
06-15-2009, 04:34 PM
Very easy to do, assuming you know how to write PHP and SQL code.

For a start making a table for each URL is a very bad idea. All you need is a single database with fields for the page name (no need to store the entire URL as they'll all be under the same domain) and the rating (probably a tinyint field, though you'll want a float (2,1) if you plan to have half stars) as fields for each record, along with a unique ID. Other optional fields include a datetime field so you know when a rating was given, and a userid field if users can be logged in.

All you need then is to add a form to your HTML (in some code that's present on every page) to submit a rating to a script that will handle the SQL code. Something along the lines of
INSERT INTO ratings (added,page,rating) VALUES ('".date().'",'".$_POST['page']."',".$_POST['rating'].")
Would work, though I'd personally recommend sanitising your data inputs first to remove any nasty strings or injections. That's pretty much all there is to it, though as I said that does rely on your having knowledge of how to pass queries to SQL and post data to PHP scripts.