Jump to content

[SOLVED] Division by zero error


graham23s

Recommended Posts

Hey gUYs,

 

in this guestbook script i'm writing i get a division by zero error, can anyone see where the problem is:

 

<?php
     ## guestbook_main.php ##############################################################
     echo '<br /><h4>Guestbook</h4>';
     echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
     echo '<table width="400" border="0" cellpadding="0" cellspacing="0" />
           <tr>
           <td align="right"><p>Your Name:</p></td><td align="left"><input type="text" name="name"></td>
           </tr>
           <tr>
           <td align="right"><p>Your E-Mail:</p></td><td align="left"><input type="text" name="email"></td>
           </tr>
           <tr>
           <td align="right"><p>Your Location:</p></td><td align="left"><input type="text" name="from"></td>
           </tr>
           <tr>
           <td align="right"><p>Your Message:</p></td><td align="left"><textarea name="message" rows="10" cols="30"/></textarea></td>
           </tr>
           <tr>
           <td align="right"> </td><td align="left"><input type="submit" name="submit" value="Sign!" /></td>
           </tr>
           </table></form>';
     echo '(<a href="guestbook.php?page=view_guestbook">View Guestbook</a>)<br /><br />';
     ## guestbook_main.php ##############################################################           
?>
<?php
     ## once the page is submitted ######################################################
     if(isset($_POST['submit'])) {
     
     ## grab the variables ##############################################################
     $name = $_POST['name'];
     $email = $_POST['email'];
     $from = $_POST['from'];
     $message = $_POST['message'];
     
     ## if there was empty fields #######################################################
     if(empty($name) || empty($email) || empty($from) || empty($message)) {
     
       echo '<p><font color="red">Error: </font>Sorry, Please Fill In All Fields!</p>';
     
     } else {
                 
     ## do the insertion ################################################################
     $guestbook_query = "INSERT INTO `guestbook` (`name`,`email`,`from`,`message`,`date`) VALUES ('$name','$email','$from','$message',now())";
     $guestbook_result = mysql_query($guestbook_query) or die (mysql_error());
     
     ## was it a success ################################################################
     if($guestbook_result) {
     
     echo '<p><font color="yellow">Success: </font>Thank You Guestbook Entry Has Been Added!<br /> (<a href="guestbook.php?page=view_guestbook">View Guestbook</a>)</p>';
     
     }
     
     
     }
     
     
     } // end of the isset //////////////////////////////////////////////////////////////
?>
<?php
     ## the get #########################################################################
     if ($_GET['page'] == "view_guestbook") {
     
     echo '<hr width="50%">';
     
        ## Pagination start #############################################################
        echo "<center>";
	// If current page number, use it 
	// if not, set one! 

	if(!isset($_GET['page'])){ 
		$page = 1; 
	} else { 
		$page = $_GET['page']; 
	} 

	// Define the number of results per page 
	$max_results = 5; 		

	// Figure out the limit for the query based 
	// on the current page number. 
	$from = (($page * $max_results) - $max_results);  

	// Perform MySQL query on only the current page number's results 

	$sql = mysql_query("SELECT * FROM `guestbook` ORDER BY `date` DESC LIMIT $from, $max_results"); 

        ## Pagination start #############################################################
     
     while($row = mysql_fetch_array($sql)) {
     
     $display_name = $row['name'];
     $display_email = $row['email'];
     $display_location = $row['from'];
     $display_message = $row['message'];
     $display_date = $row['date'];
     
     ## a nice table ####################################################################
     echo '<table width="400" border="1" bordercolor="#0000000" cellpadding="5" cellspacing="0" />
           <tr>
           <td><p>'.$display_name.' Posted On (<font color="yellow">'.$display_date.'</font>)</p></td>
           </tr>
           <tr>
           <td bgcolor="#000000" align="left"><p>'.$display_message.'</p></td>
           </tr>
           <tr>
           <td align="right"><p>'.$display_email.'</p></td>
           </tr>
           </table><br />';        
     }
          
     echo '<hr width="50%">';     

     } 
         ## Pagination end ###############################################################
	// Figure out the total number of results in DB: 
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `guestbook`"),0); 

	// Figure out the total number of pages. Always round up using ceil() 
	$total_pages = ceil($total_results / $max_results); 

	// Build Previous Link 
	if($page > 1){ 
		$prev = ($page - 1); 
		echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<< </a> "; 
	} 

	for($i = 1; $i <= $total_pages; $i++){ 
		if(($page) == $i){ 
			echo "[<b>$i</b>] "; 
			} else { 
				echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
		} 
	} 

	// Build Next Link 
	if($page < $total_pages){ 
		$next = ($page + 1); 
		echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"> >>></a>"; 
	} 
	echo "<br /><br />"; 

        ## Pagination end ###############################################################   
?> 

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/
Share on other sites

This line is obviously returning 0.

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `guestbook`"),0);

 

I'm not familiar with using mysql_result(), so I can't really help you on fixing that.

 

You could use that query, then to get the number of results just use mysql_num_rows().

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.