JeremyH
07-22-2008, 05:45 PM
I have a problem making my web page work.
My database has 2 fields: "catalog", which is an incremental number and corresponds to an image (ie "1.jpg","2.jpg",etc), and "rating", which has the item's score, which is what I want updated as users rate the item it corresponds to.
My page displays the items based on its score--highest "rating" value on top, and then decends as the "rating" value decreases. Each item has a <select><option> pull down menu where the user can give it a score. When the user hits SUBMIT, all the votes are stored in an array called "$rec".
The items in $rec are ordered based on how they were displayed on the voting page. I decided in the processing script to once again order things by rating, and then UPDATE each rating field by adding the value given it by the user. Here is my processing script:
$rec = $_POST['rec'];
$query = "SELECT * FROM `Items` ORDER BY `rating` DESC";
$result = mysql_query($query) or die(mysql_error());
$num = 0;
while($row = mysql_fetch_array($result)){
$newrank = "UPDATE `Items` SET `rating`=`rating` + $rec[$num]";
$resultTwo = mysql_query($newrank);
$num++;
}
This does not work. If I rate all the items with different scores, sometimes the database will not update at all. If I only score one item and leave the others blank (with a default of 0), then it assigns every item that one score. For instance, for the first item the user scores it "8" and doesn't vote on anything else. When SUBMIT is hit, I get $rec(8,0,0,0,0,0,0,etc...). That script will process, but each value in all rating fields has been increased by 8! Can anyone see what I am doing wrong? I am a MySQL rookie, and this is an exercise to get familiar with it, but I'm hung up on this step.
Thanks for any help.
Jeremy
My database has 2 fields: "catalog", which is an incremental number and corresponds to an image (ie "1.jpg","2.jpg",etc), and "rating", which has the item's score, which is what I want updated as users rate the item it corresponds to.
My page displays the items based on its score--highest "rating" value on top, and then decends as the "rating" value decreases. Each item has a <select><option> pull down menu where the user can give it a score. When the user hits SUBMIT, all the votes are stored in an array called "$rec".
The items in $rec are ordered based on how they were displayed on the voting page. I decided in the processing script to once again order things by rating, and then UPDATE each rating field by adding the value given it by the user. Here is my processing script:
$rec = $_POST['rec'];
$query = "SELECT * FROM `Items` ORDER BY `rating` DESC";
$result = mysql_query($query) or die(mysql_error());
$num = 0;
while($row = mysql_fetch_array($result)){
$newrank = "UPDATE `Items` SET `rating`=`rating` + $rec[$num]";
$resultTwo = mysql_query($newrank);
$num++;
}
This does not work. If I rate all the items with different scores, sometimes the database will not update at all. If I only score one item and leave the others blank (with a default of 0), then it assigns every item that one score. For instance, for the first item the user scores it "8" and doesn't vote on anything else. When SUBMIT is hit, I get $rec(8,0,0,0,0,0,0,etc...). That script will process, but each value in all rating fields has been increased by 8! Can anyone see what I am doing wrong? I am a MySQL rookie, and this is an exercise to get familiar with it, but I'm hung up on this step.
Thanks for any help.
Jeremy