Forum Moderators: coopster
A little background on what I'm trying to do here. Right now it's essentially a counter. Other scripts do the actual sorting of the information. Every entry starts out with zero points, and as users run this script, the point value is supposed to raise by one. My problem is that it will go up to one, and then not raise any higher. I'm thinking there might be an easier way to write this, and if there is, please correct me.
<?php
require_once 'includes/header.inc.php';$ref = getenv("HTTP_REFERER");
$refexplode = explode("=", $ref);
$result = mysql_query("SELECT rating FROM topics WHERE topic_id = $refexplode[1];");
if (!$result) {
die('<p>Error performing query. Please submit a bug report about this error.' . mysql_error() .
'</p>');
}
//Do unnecessesary SQL work because I suck, this needs a redo btw
while ( $row = mysql_fetch_array($result) ) {
echo('<p>' . $row['u'] . '</p>');
}
$ratingnum = $row['rating'];
$addedratingnum = $ratingnum - 1;
$result1 = mysql_query("UPDATE `topics` SET `rating` = $addedratingnum WHERE `topic_id` = $refexplode[1];");
if (!$result1) {
die('<p>Error performing query. Please submit a bug report about this error.' . mysql_error() .
'</p>');
}
header( 'Location: '. $ref ) ;
?>
Thanks for any help!
The while loop in your code is "using up" the data from your query. The while loop will keep executing until mysql_fetch_array returns FALSE. Since you're assigning FALSE to $row, that means by the time you get to:
$ratingnum = $row['rating'];
there is no $row['rating'], so at best you're saying:
$addedratingnum = FALSE - 1;
I also don't understand why you're subtracting one when your text says you want to add one.
If topic_id is unique, that is, you would never have more than one record with the same topic_id, then don't do the while, just do this one time:
$row = mysql_fetch_array($result);
You can also do it in one fell swoop instead of getting the record, adding to the rating, and updating:
"UPDATE `topics` SET `rating` = `rating` + 1 WHERE `topic_id` = {$refexplode[1]}"
When you refer to an array inside a quoted string, you should enclose it with braces.