<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1030087" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1030087</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/rating-script-wont-update-database-problem#comment-1175972</link>
    <description> &lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;p&gt;&lt;strong&gt;aboyd wrote:&lt;/strong&gt; You took $siteid directly from $_POST!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Dont be so shocked. &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/tongue.png&quot; title=&quot;Sticking out tongue&quot; alt=&quot;Sticking out tongue&quot; class=&quot;smiley-content&quot; /&gt; Coding in general should be done iteratively and incrementally, and each test-case accounted for.  &lt;/p&gt;
&lt;p&gt;If I know he is passing a number, should I check to see if a lazy &quot;?&quot; floated in the mix?  Probably completely unnecessary...probably.  &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/tongue.png&quot; title=&quot;Sticking out tongue&quot; alt=&quot;Sticking out tongue&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
&lt;p&gt;But if you are insistant...Since he never passes anything but 0-9, then a simpler &quot;else die&quot; would suffice.  &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/big.png&quot; title=&quot;Laughing out loud&quot; alt=&quot;Laughing out loud&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
 </description>
     <pubDate>Wed, 03 Aug 2005 01:07:31 +0000</pubDate>
 <dc:creator>jjinno</dc:creator>
 <guid isPermaLink="false">comment 1175972 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/rating-script-wont-update-database-problem#comment-1175270</link>
    <description> &lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;p&gt;&lt;strong&gt;Python wrote:&lt;/strong&gt; $result = mysql_query(&quot;SELECT `rating`, `numvotes` FROM `reviews_site` WHERE `id` = $siteid&quot;);&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;:::boggle:::&lt;/p&gt;
&lt;p&gt;You took $siteid directly from $_POST!  PHP is pretty good at preventing SQL injection, but shouldn&#039;t that data get SOME laundering?  Even just this:&lt;/p&gt;
&lt;p&gt;$siteid = preg_replace(&#039;/[^0-9]+/&#039;, &#039;&#039;, $siteid);&lt;/p&gt;
&lt;p&gt;-Tony&lt;/p&gt;
 </description>
     <pubDate>Sun, 24 Jul 2005 08:31:52 +0000</pubDate>
 <dc:creator>aboyd</dc:creator>
 <guid isPermaLink="false">comment 1175270 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/rating-script-wont-update-database-problem#comment-1175262</link>
    <description> &lt;p&gt;First, see &lt;a href=&quot;http://us2.php.net/manual/en/language.types.array.php#language.types.array.donts&quot; class=&quot;bb-url&quot;&gt;http://us2.php.net/manual/en/language.types.array.php#language.types.array.donts&lt;/a&gt; for why to use &lt;strong&gt;$_POST[&#039;siteid&#039;]&lt;/strong&gt; instead of &lt;strong&gt;$_POST[siteid]&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt; As to why your database isn&#039;t updating. Here is my #1 suggestion to anyone who says a database isn&#039;t updating right (or not returning data you expect).&lt;/p&gt;
&lt;p&gt;   Right before the line with &lt;strong&gt;mysql_query&lt;/strong&gt; add this line: &lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;&amp;lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;print \&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&quot;&amp;lt;tt&amp;gt;&amp;lt;pre&amp;gt;\nSQL=&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$query&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;\n&amp;lt;/pre&amp;gt;&amp;lt;/tt&amp;gt;\n\&quot;;&lt;br /&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;This will display the query string as it is being sent to the server. The &lt;/p&gt;
&lt;pre&gt; is to help determine any spacing problems, not really needed but I like them. Once you get the bugs out, comment out this line.
   
 If this looks right, my suggestion then would be to go into phpMyAdmin (or my preference, log into the DB server itself) and manually execute the sql statement displayed by the above. This sometimes will give you better errors, and see results better if you don&#039;t have them coded properly to give results.
   
   However, in this case, I took your code, copied and pasted it into Zend Studio, told it to analize it, and bingo...  
   
   &lt;strong&gt;Global variable $final  was used before it was defined (line 16)
   &lt;/strong&gt;( I added a &amp;lt;? line, so line 16 was the line where you define the $query)
   
 You were probably wanting $final2 in the SQL statement. If you added the print statement above, you would have seen that the value to update the database with was missing.
   
   Also, in your &lt;strong&gt;update&lt;/strong&gt; statement, you need to separate the values with a comma, not &lt;strong&gt;&amp;amp;&amp;amp;:
   
   &lt;/strong&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;&amp;lt;?php&lt;br /&gt;$query &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= \&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&quot;UPDATE `reviews_site` SET `rating` = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$final&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;, `numvotes` = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$newnumvotes&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; WHERE `id` = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$siteid&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;\&quot;;&lt;br /&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
  BTW, to make coding more simplified, you can reduce your lines to get the data from the database (the whole &lt;strong&gt;while&lt;/strong&gt; statement) to just this line:
   &lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;&amp;lt;?php&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;list(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$currating&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$curnumvotes&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;) = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;mysql_fetch_assoc&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;);&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
   I&#039;m assuming that there should only be one record with each site ID.
   
 Next, lets take a look at your math logic behind your ratings. You are taking the previous rating, adding the current rating, then dividing by the total # of ratings. This will (after the first 2 ratings) give you the wrong numbers. You need to take the total of ALL the ratings then divide them to get the average. So in your database, you should be storing TotalRating and NumVotes, then when you display the rating, do 
   &lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;&lt;span style=&quot;color: #000000&quot;&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;&amp;lt;?php&lt;br /&gt;$query &lt;/span&gt;&lt;span style=&quot;color: #007700&quot;&gt;= \&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;&quot;SELECT (`TotalRating`/`NumVotes`) AS AvgRating FROM `reviews_site` WHERE `id` = &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$siteid&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;\&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt; = mysql_query(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$query&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp; list(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$avgRating&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;) = mysql_fetch_array(&lt;/span&gt;&lt;span style=&quot;color: #0000BB&quot;&gt;$result&lt;/span&gt;&lt;span style=&quot;color: #DD0000&quot;&gt;);&lt;br /&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
   
   See the image below (or &lt;a href=&quot;http://http://66.39.121.82/ratings.gif&quot; class=&quot;bb-url&quot;&gt;look here if this d&lt;/a&gt;&lt;a href=&quot;http://http://66.39.121.82/ratings.gif&quot; class=&quot;bb-url&quot;&gt;oesn&#039;t display right&lt;/a&gt;) to see sample data and how it would calculate differently.
   &lt;img src=&quot;http://66.39.121.82/ratings.gif&quot; alt=&quot;&quot; class=&quot;bb-image&quot; /&gt;
   
   As you can see, the more you are rated, the worst your rating will be. It can never be higher than &lt;strong&gt;10&lt;/strong&gt; (possible 5 for previous average, plus current 5) &lt;strong&gt;divided by total votes. &lt;/strong&gt;And actually, after the 3rd vote, it can never reach 10. Even if you have a perfect vote of 5 each time, here is what you end up with: (&lt;a href=&quot;http://66.39.121.82/ratings2.gif&quot; class=&quot;bb-url&quot;&gt;alt link if img doesn&#039;t display here&lt;/a&gt;)
  &lt;img src=&quot;http://66.39.121.82/ratings2.gif&quot; alt=&quot;&quot; class=&quot;bb-image&quot; /&gt;

 Lastly, I would recommend breaking up your beginning sql statements into a $query variable and then a line to execute the sql. Then if needed you could the print statement if needed.
   
   Good luck with your code, let us know if you need any more help.
   
   -Greg &lt;/pre&gt;</description>
     <pubDate>Sun, 24 Jul 2005 01:42:49 +0000</pubDate>
 <dc:creator>Greg K</dc:creator>
 <guid isPermaLink="false">comment 1175262 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
