Jump to content

PHP Breaking HTML Doc


Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by Ch0cu3r
Added code tags
Link to comment
https://forums.phpfreaks.com/topic/292442-php-breaking-html-doc/
Share on other sites

  • Solution

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 by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.