Rating script wont update database problem
I have this:
<?php
$siteid = $_POST[siteid];
$newrating = $_POST[rating];
$result = mysql_query(\"SELECT `rating`, `numvotes` FROM `reviews_site` WHERE `id` = $siteid\");
while($rate=mysql_fetch_array($result)){
$curnumvotes = $rate[numvotes];
$currating = $rate[rating];
}
$curnumvotes++;
$newnumvotes = $curnumvotes;
$final1 = ($currating + $newrating); // add old and new
$final2 = ($final1/$newnumvotes); // get average
$final2 = number_format($final2, 2, '.', '');
$query = \"UPDATE `reviews_site` SET `rating` = $final && `numvotes` = $newnumvotes WHERE `id` = $siteid\";
$uresult = mysql_query($query);
?>
$_POST[siteid] is equal to 2
$_POST[rating] is equal to any number from 0-5, depends in what is submitted at form.
This basically is supposed to take the original rating value, add it to the new submitted rating value and then divide it by the number of votes.
Ive echoed it all out step by step to see if its doing the calculations and it does but it wont update the database with the new values.
Any suggestions?
UK Website Design
http://www.pythondesigns.com
Greg K posted this at 01:42 — 24th July 2005.
He has: 2,145 posts
Joined: Nov 2003
First, see http://us2.php.net/manual/en/language.types.array.php#language.types.array.donts for why to use $_POST['siteid'] instead of $_POST[siteid]
As to why your database isn't updating. Here is my #1 suggestion to anyone who says a database isn't updating right (or not returning data you expect).
Right before the line with mysql_query add this line:
<?php
print \"<tt><pre>\nSQL=$query\n</pre></tt>\n\";
?>
aboyd posted this at 08:31 — 24th July 2005.
They have: 33 posts
Joined: Nov 2004
:::boggle:::
You took $siteid directly from $_POST! PHP is pretty good at preventing SQL injection, but shouldn't that data get SOME laundering? Even just this:
$siteid = preg_replace('/[^0-9]+/', '', $siteid);
-Tony
jjinno posted this at 01:07 — 3rd August 2005.
They have: 18 posts
Joined: Jul 2004
Dont be so shocked. Coding in general should be done iteratively and incrementally, and each test-case accounted for.
If I know he is passing a number, should I check to see if a lazy "?" floated in the mix? Probably completely unnecessary...probably.
But if you are insistant...Since he never passes anything but 0-9, then a simpler "else die" would suffice.
- Jinno
-----------------------
"I hear and I forget. I see and I remember. I do and I understand." - Confucius
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.