Current location: Hot Scripts Forums » Programming Languages » PHP » Hi, it's me with a rating script again ^_^;


Hi, it's me with a rating script again ^_^;

Reply
  #1 (permalink)  
Old 12-29-03, 04:51 PM
miki miki is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hi, it's me with a rating script again ^_^;

Hey there,

I have this PHP/MySQL ratingscript;
- The first file (view.php) makes a list of filenames that needs to be rated.
- When someone presses the submit button and chose a certain rating, the $rate variable goes to file 2 (rate.php)
- The script in rate.php calculates the new rating. And displays the new rating on the site.


MySQL table (rates):
rate_id | filename | num_votes | votes | rating

--------------------------------------------------

File 1 (view.php):
PHP Code:

<?PHP

include ($rootdir 'scripts/dbconnect.inc.php');
mysql_select_db("site"$db);
$rates_SQL="SELECT * from rates";
$rates_resultmysql_query($rates_SQL) or die 
(
"Could not execute query : $rates_SQL." mysql_error());
?>

<?php while ($row=mysql_fetch_array($rates_result)) { ?>

Rating <?php echo $row['filename'?>: <form action="rate.php?id=
<?php echo $row['rate_id'?>" method="post">
<SELECT name="rate">
<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="rate">
</FORM>

<?php ?>
File 2 (rate.php):
PHP Code:

<?php

include ($rootdir 'scripts/dbconnect.inc.php');
mysql_select_db("site"$db);
$rates_SQL "SELECT * FROM rates WHERE rate_id='$id'";
$rates_result mysql_query ($rates_SQL) or die 
(
"Could not execute query : $rates_SQL." mysql_error());
?>

<?php while ($row=mysql_fetch_array($rates_result)) {
$id mysql_escape_string($_GET['id']); 
$filename $row['filename'];
$num_votes $row['num_votes'];
$votes $row['votes'];
$rating $row['rating'];

$new_votes $num_votes+1;
$votes $votes+$rate;
$rating round(($votes/$new_votes),2); 

$rates_SQL"UPDATE rates SET num_votes='$new_votes', votes='$votes', 
rating='
$rating' WHERE rate_id='$id'";
$rates_result=mysql_query($rates_SQL) or die 
(
"Could not execute query : $rates_SQL." mysql_error());

if (
$rates_result) {
echo 
"Thank you. The article $filename has rating = $rating after your vote."; } 

?>


Looking good, works fine.. BUT (hates this "but" -_-
then the script says:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
resource in /public_html/scripts/rate.php on line 9
(That should be line: <?php while ($row=mysql_fetch_array($rates_result))
{
)?

I just can't seem to find the problem.. *scratches head*
I hope somebody does.. thanks in advance! ^_^

*waves~~

Last edited by miki; 12-29-03 at 04:54 PM.
Reply With Quote
  #2 (permalink)  
Old 12-29-03, 05:54 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
err
$rates_SQL = "SELECT * FROM rates WHERE rate_id='$id'";

where is it getting its '$id' from???
Reply With Quote
  #3 (permalink)  
Old 12-30-03, 07:05 AM
miki miki is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
From file1 (view.php)..

PHP Code:

Rating <?php echo $row['filename'?>: [b]<form action="rate.php?id= 

<?php echo $row['rate_id'?>" method="post"> [/b]
<SELECT name="rate"> 
<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="rate"> 
</FORM>
Reply With Quote
  #4 (permalink)  
Old 12-30-03, 07:37 AM
michaeln michaeln is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
This line:
$id = mysql_escape_string($_GET['id']);
Should go above your first SQL Query. If you are on a server with register_globals = off, as they should be, your first mysql_query is returning nothing because you are passing it an ID of 0.

You should also change this line:
$votes = $votes+$rate;
to
$votes = $votes+$_POST['rate'];
or put this line somewhere above it:
$rate = $_POST['rate'];


Also,
If I am reading your script correctly you are always going to get one row returned in rate.php so you do not need the while loop around the fetch_array. You pass mysql an ID and tell it to return that row. If, I assume, you have mysql auto incrementing the ID with every page you add and you only have one row per page you will have a problem if it returns more than one row seeing as your users can only vote on one page at a time. Geeze that was a run on sentence. LOL
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a good review management script griz_fan Script Requests 10 07-29-07 05:08 AM
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 12:12 AM
Picture rating script & Buddy Icon Script for sale! kubserv General Advertisements 0 11-04-03 07:16 PM
point rating script SPIKE326 PHP 0 08-28-03 07:29 PM
a Linker script with rating, popularity & reviews Salvage Script Requests 2 08-28-03 01:00 PM


All times are GMT -5. The time now is 07:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.