jupiter Posted November 13, 2014 Share Posted November 13, 2014 (edited) I'm experimenting with a basic CRUD App. The table created by the code below renders, however when I view source, the last tag is the closing </tr> after the while loop finishes. There's nothing after that. No closing table, html, or body tags. I can't see what I've done wrong. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Eric's PHP CRUD App</title> <style type="text/css"> table { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; text-align: center; margin: 0 auto; } table td { text-align:center; border: 1px solid #dfdfdf; } tr:nth-child(odd) { background: #fdfdfd; } tr:nth-child(even) { background: #B8D3FF; } th { background-color:#ccc; } .center { width: 1050px; margin:0px auto; } h1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; padding-bottom: 15px; text-align: center; margin: 0 auto; color: white; } </style> </head> <body bgcolor="#999"> <?php require_once("db_connect.php"); echo "<h1>Update Student Records</h1>"; echo "<table cellpadding='10'>"; echo "<tr bgcolor='#cccccc'>"; echo "<th>First Name</th>"; echo "<th>Last Name</th>"; echo "<th>Test 1 Grade</th>"; echo "<th>Test 2 Grade</th>"; echo "<th>Test 3 Grade</th>"; echo "<th>Test 4 Grade</th>"; echo "<th>Final Exam Grade</th>"; echo "<th>Average Grade</th>"; echo "<th>Final Grade</th>"; echo "<th>delete</th>"; echo "<th>edit</th>"; echo "</tr>"; $sql = "SELECT * FROM students"; $result = mysqli_query($link, $sql) or die(mysql_error()); while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) or die(mysql_error())) { $final = ""; $average = (($row["test1Grade"]) + ($row["test2Grade"]) + ($row["test3Grade"]) + ($row["test4Grade"]) + ($row["finalExamGrade"])) / 5; if ($average >= 90) { $final = "A"; } else if ($average >= 80) { $final = "B"; } else if ($average >= 70) { $final = "C"; } else if ($average >= 60) { $final = "D"; } else if ($average < 60) { $final = "F"; } echo "<tr>"; echo "<td>" . $row["firstName"] . "</td>"; echo "<td>" . $row["lastName"] . "</td>"; echo "<td>" . $row["test1Grade"] . "</td>"; echo "<td>" . $row["test2Grade"] . "</td>"; echo "<td>" . $row["test3Grade"] . "</td>"; echo "<td>" . $row["test4Grade"] . "</td>"; echo "<td>" . $row["finalExamGrade"] . "</td>"; echo "<td>" . round($average) . "</td>"; echo "<td>" . $final . "</td>"; echo "<td><a href='delete.php?id=" . $row['studentID'] . "'>›</a></td>"; echo "<td><a href='update.php?id=" . $row['studentID'] . "'>›</a></td>"; echo "</tr>"; } echo "</table>"; ?> <a href='insert.php'>Add new student</a> </body> </html> Edited November 13, 2014 by Ch0cu3r Added code tags Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted November 13, 2014 Solution Share Posted November 13, 2014 (edited) Try removing or die(mysql_error()) as part of the while loop condition! That should not be there. In fact what is more than likely happening is PHP is getting to the last row and killing the script. This happens becuase mysqli_fetch_array() will return false when there are no more rows to fetch from the resultset and so the or die statement will trigger and this will kill the script. Also please wrap your code in or click the <> button in the editor when posting code. EDIT Why are using mysql_error()? The mysql functions and mysqli functions are not compatible with each other. You should not be using the old mysql_* functions as they are deprecated meaning they are no longer supported. You should stick to just using the mysqli functions. Edited November 13, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
jupiter Posted November 13, 2014 Author Share Posted November 13, 2014 @Ch0cu3r Thanks. That worked. I replaced: $result = mysqli_query($link, $sql) or die(mysql_error()); with $result = mysqli_query($link, $sql) or die('Connect Error: ' . mysqli_connect_error()); and my page is rendering completely. Quote Link to comment 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.