graham23s Posted June 30, 2007 Share Posted June 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/ Share on other sites More sharing options...
Barand Posted June 30, 2007 Share Posted June 30, 2007 The error message should also give you a line number. Somewhere you have divided by a variable that is either unset or has a zero value. Check variables are non zero before using them as a divisor. Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286771 Share on other sites More sharing options...
pocobueno1388 Posted June 30, 2007 Share Posted June 30, 2007 It's got to be this line right here: <?php $total_pages = ceil($total_results / $max_results); ?> Do you have any rows in the "guestbook" table of your database? If not, that is most likely your problem. Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286772 Share on other sites More sharing options...
graham23s Posted June 30, 2007 Author Share Posted June 30, 2007 Hi Guys, the error is on line 155 which is exactly the line pocu said. i have 14 entries in the guestbook , i just can work it out. Graham Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286799 Share on other sites More sharing options...
pocobueno1388 Posted June 30, 2007 Share Posted June 30, 2007 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(). Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286809 Share on other sites More sharing options...
graham23s Posted June 30, 2007 Author Share Posted June 30, 2007 Hi Mate, the thing is if i echo out $total_results it displays 14 which is right could that still be the problem? cheers Graham Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286813 Share on other sites More sharing options...
Barand Posted June 30, 2007 Share Posted June 30, 2007 If it's a division by zero, it's $max_results that is zero. That line the 119th line of posted code, so is there something in the lines you didn't post that sets it to zero? Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286815 Share on other sites More sharing options...
Barand Posted June 30, 2007 Share Posted June 30, 2007 Also, $max_results is set only if $_GET['page'] == 'view guestbook' Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286819 Share on other sites More sharing options...
graham23s Posted June 30, 2007 Author Share Posted June 30, 2007 got it, thanks a lot guys:) Graham Quote Link to comment https://forums.phpfreaks.com/topic/57871-solved-division-by-zero-error/#findComment-286841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.