Forum Moderators: coopster

Message Too Old, No Replies

Modifying Database Information

rate-a-product system

         

itechdesigns

8:47 pm on Feb 11, 2006 (gmt 0)

10+ Year Member



Okay, so this is how it works, I have a list of products and they each have their own rating stored in a database. There are two columns which determine the average rating: total score and total # of votes. The theory is to take the total score and divide it by the total # of votes. So I do this:
$rating=mysql_result($result,$page_num,"rating");
$votes=mysql_result($result,$pagenum,"votes");
$arating=$rating / $votes;

So variable 'arating' is the avaerage rating, which I then display on the product page.

Now I need the customer to be able to add their vote to the rating system. Here is what I do:

echo"Rating: $arating<br><form action='http://www.examplesite.com/business.php' method='post'>
<select name='ud_urate'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select><input type=submit value='Submit'>
</select>";

Okay that is the form. Now it is supposed to take their entered value and add it to the old rating.
$ud_rate=$rating+ud_rate;
Then send the new information back to the database, and then when the page is reloaded the database is updated.
$query = "UPDATE business SET rating = '$ud_urate' WHERE id = '$page_num'";
mysql_query($query);

Unfortunatley, this does not update the database. It just remains the same.
FYI: $page_num is determined earlier on by a query string in the URL and helps to determine what the starting product in the list is.
Thanks for any help.

Dijkgraaf

9:05 pm on Feb 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Echo the SQL statement out to the page, so you can see what it actually is just before it tries and writes it to the database. It may not be what you are expecting.
BTW I would have though both the rating and id would be numeric, so why are you put quotes around them?

itechdesigns

9:39 pm on Feb 11, 2006 (gmt 0)

10+ Year Member



okay i think that the issue is that it is all on one page and therefore the entire code is executed before the form is submitted. So, when the page loads it executes everything in order:
echo"Rating: $arating<br><form action='http://www.examplesite.com/business.php' method='post'>
<select name='ud_urate'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select><input type=submit value='Submit'>
</select></form>";
$ud_rate=$rating+ud_rate;
$query = "UPDATE business SET rating = '$ud_urate' WHERE id = '$page_num'";
mysql_query($query);

The problem is that I need the part that updates the database only to happen when the form is submitted. Now I thought of just making a new php file and executing it from there (form action='newpage.php'). However, the problem is that this is the scripting for 1 of 9 products on the page. So, the new page wont be able to tell which product is being rated. How can I transfer the value of one variable on the original page to a new page? Or is there another way to do this?

Dijkgraaf

8:49 pm on Feb 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need in if block.
e.g.
if($action == "Submit"){
... do things from after the form submit
} else
{
... do things for page load
}

dreamcatcher

10:24 pm on Feb 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, if you have register globals OFF, you need to access the variables using $_POST & $_GET.

dc

Anyango

6:38 am on Feb 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




The problem is that I need the part that updates the database only to happen when the form is submitted. Now I thought of just making a new php file and executing it from there (form action='newpage.php'). However, the problem is that this is the scripting for 1 of 9 products on the page. So, the new page wont be able to tell which product is being rated. How can I transfer the value of one variable on the original page to a new page? Or is there another way to do this?

simple!

when you make form like

echo "<form action=newpage.php>";

make a hidden field in that form containing the id of the product for which rating is being made like

echo "<form action=newpage.php>";
echo "<input type=hidden name=productid value='$id'>";

take product id from mysql and put it over there, so when newpage.php gets called it can update rating for that very product.

this was just to answer that question, the above two suggestions by those senior guys are much more recommended.