simple rating script errors Warning: mysql_num_rows(): suppl

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
farkewie
Forum Newbie
Posts: 22
Joined: Sat Jun 02, 2007 11:25 pm

simple rating script errors Warning: mysql_num_rows(): suppl

Post by farkewie »

Hi have am new to php this is one of my first hand writen script its just a basic rating script, im getting some errors i cant work out, any help would be great. google says it may be because the script cant find the database but it is because it told me it couldnt find some colums, but when i got rid of the spelling mistakes im still getting errors.

below is my code and erors

Code: Select all

<?php
$id = $_POST['id'];

//Grabing the details from the name table

$connection=mysql_connect("localhost","username", "password") or die("Unable to connect!"); /* change this! */ 

mysql_select_db("database") or die("Unable to select database!"); /* change this! */ 

$result = "SELECT * FROM name WHERE id='$id'";
$numrow = mysql_num_rows ($result);

// converting thenm into objects
$row = mysql_fetch_array($result);

$domain = GetHostByName($REMOTE_ADDR);
$submit_rating = $_POST['rate'];
$num_votes = $row["num_votes"];
$votes_total = $row["votes_total"];
$new_num_votes =  ($num_votes + 1);
$new_votes_total = ($votes_total + $submit_rating);
$rating = (new_votes_total / $new_num_votes);


 $query2 = "UPDATE name SET `num_votes` = '$new_num_votes',
`votes_total` = '$new_votes_total',
`rating` = '$rating'" ;


$result2=mysql_query($query2) or die("Error in query:".mysql_error()); 
//if ($result) 
    //echo mysql_affected_rows()." row inserted into the database effectively."; 

//  CLOSE CONNECTION ---> 

header ("Location:".$_SERVER['HTTP_REFERER']." ");



mysql_close($connection); 
?>

errors:

Code: Select all

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/tyspicsc/public_html/posthate/rating_action.php on line 11

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/tyspicsc/public_html/posthate/rating_action.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at /home/tyspicsc/public_html/posthate/rating_action.php:11) in /home/tyspicsc/public_html/posthate/rating_action.php on line 36
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: simple rating script errors Warning: mysql_num_rows(): s

Post by tecktalkcm0391 »

Try this....
you don't have any error checking to make sure the select went through, plus you didn't even to a query... look below

Code: Select all

<?php
$id = $_POST['id'];

//Grabing the details from the name table

$connection=mysql_connect("localhost","username", "password") or die("Unable to connect!"); /* change this! */ 

mysql_select_db("database") or die("Unable to select database!"); /* change this! */ 

$sql = "SELECT * FROM name WHERE id='$id'";   // HERE IS THE QUERY YOU WANT TO DO BUT YOU HAVEN'T DONE IT YET....
$result = mysql_query($sql);  // YOU NEED THIS IN ORDER TO MAKE IT WORK!
//THEN YOUR SHOULD MAKE SURE IT WORKS...
if(!$result){ // IF THE RESULT FAILS
     echo mysql_error(); // THIS PRINTS OUT THE MYSQL ERROR... BUT DO NOT LEAVE ON A LIVE SITE. IT ALLOWS HACKERS TO GET A LOOK AT YOUR DATABASE STRUCTURE AND YOUR MYSQL QUERY
} else {
$numrow = mysql_num_rows ($result);

// converting thenm into objects
$row = mysql_fetch_array($result);

$domain = GetHostByName($REMOTE_ADDR);
$submit_rating = $_POST['rate'];
$num_votes = $row["num_votes"];
$votes_total = $row["votes_total"];
$new_num_votes =  ($num_votes + 1);
$new_votes_total = ($votes_total + $submit_rating);
$rating = (new_votes_total / $new_num_votes);


 $query2 = "UPDATE name SET `num_votes` = '$new_num_votes',
`votes_total` = '$new_votes_total',
`rating` = '$rating'" ;


$result2=mysql_query($query2) or die("Error in query:".mysql_error()); 
//if ($result) 
    //echo mysql_affected_rows()." row inserted into the database effectively."; 

//  CLOSE CONNECTION ---> 

header ("Location:".$_SERVER['HTTP_REFERER']." ");



mysql_close($connection); 
}

?>
Post Reply