Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment you will tackle debugging code written by another programmer.

ID: 3797983 • Letter: I

Question

In this assignment you will tackle debugging code written by another programmer. Programmers working in industry often have to extend/test/debug code written by other programmers. It is not an easy task, especially when the code does not have proper comments, or simply looks "messy". It is especially difficult if you have to debug the code across several files, as in this case, html and php files. John Novice is not using modular approach while writing his code, and as the result he spends a lot of time debugging. He also does not thoroughly test his code, so runtime errors and incorrect output are always a possibility. This time you will play the role of a teacher of the programming course that John is taking at the Granite College. You will figure out what is wrong with John's code, fix all the errors, and determine if any runtime errors are present. You will then submit the corrected code, provide Johnny with helpful feedback including a list of errors that you have found. Every time you discover and fix an error, please write it down, so you do not forget about later. You will also list other modifications/corrections you have made to the code: formatting, commenting, changing variable names, etc. Johnny's code is attached.

postreview.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Book Review Board</title>               <!page title!>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<?php

if (isset($_POST['submit'])) {
   $rname = stripslashes($_POST['rlname']);      
                                              
   $book = stripslashes($_POST['book']);
   $email = stripslashes($_POST['email']);
      
   $rname = str_replace(",", "-", $rname);
   $book = str_replace(",", "-", $book1)
   $email = str_replace(",", "-", $email);
   $existingname = array();
   if (file_exists(  
       "rreviewed.txt") &&
       filesize("reviewed.txt") > 0) {
           $reviewarray = file(
           "rleviewed.txt");
       $count = count($reviewarray);
       for ($i = 0; $i < $count; ++$i) { //importing the previous text file into array
           $currbook = explode(",",
               $reviewarray[$i])
           $existingname[] = $currbook[0];
   }
}
if (in_array($rname, $existingname)) {
                                      
       echo "<p>The name you entered already exists!<br /> ";
       echo "Please enter a new name and
           try again.<br /> ";
       echo "Your message was not saved.</p>";
       $rname = "";
}
else {  
   $reviewrecord =
       "$rname, $book, $email ";
   $reviewfile =
       fopen("reviewed.tx","ab");
   if ($reviewfile ==== FALSE )   //if error saving, responds back
       echo "There was an error saving your review! ";
   else {   //if no error, writes record to file
           fwrite($reviewfile , $reviewrecord);
           fclose($reviewfile);
           echo "Your review has been saved. ";
           $rname = "";
           $book = "";
   }
}
}
else {   //if new page load, then there is nothing for the variables,
       //so this effectively zero's them out to start with
   $rbname = "";
   $book = "";
   $email = "";
}
?>
<h1>Post New Review</h1>
<hr />
<form action="posterreview.php" method="POST"> <!input of new data!>
<span>Name:</span>
   <input type="text" name="rname" /><br />
   <p>   <!simple formating to make it more presentable!>
<span>Book:</span>
   <input type="text" name="books" /><br />
   <p>
<span>Email:</span>
   <input type="text" name="email" /><br />
   <p>
<input type="submit" name="submit" value="Post Review" />
<input type="reset" name="reset" value="Reset Form" />
</form>
<hr />
<p>
<a href="reviewboard.php">View books reviewed</a> <!hyperlink to view all submitted so far!>
</p>
</body>
</html>

reviewboard.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Book Review Board</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<h1>Book Review Board</h1>
<?php
//checks if page was previously loaded
if (isset($_GET['action'])) {  
                              
   if ((file_exists(
       "reviewedd.txt")) &&
       (filesize(
       "reviewed.txt") != 0)) {  
       $reviewarray = file(
           "reviewed.txt");
       if (count($reviewarray)>0) {  
           $newreview =
               implode($reviewarray){}
           $reviewstore = fopen(   //opens file for
               "reviewed.txt",   "wb");      
           if ($reviewstore === false)      
               echo "There was an error
               updating the review file ";
           else {       //if no error, writes data to file
               fwrite($reviewstore,
                   $newreview);
           fclose($reviewstore);
           }
           }
       }
       else   //if file existed, but was zero bytes, releases the file back without write
           unlink("reviewed.txt");
   }
}
if ((!file_exists("reviewed.txt"))       //if file does not exist, or equals zero bytes
   || (filesize("reviewed.txt")
   == 0))
   echo "<p>There are no books reviewed.</p> ";
else {       //if all is good, begins to build the table to display the info from the text file
   $reviewarray =
       file("reviewed.txt");
   echo "<table
         //sets background color
       border="1" width="100%"> ";
   $count = count($reviewarray);
   for ($i = 0; $i < $count; ++$i) {       //runs through each line of data from the file
       $currbook = explode(",",
       $reviewarray[$i]);
   echo "<tr> ";       //new table row
   echo "<td width="5%"
      >" .
       ($i + 1) . "</td> ";
   echo "<td width="95%"><span   //new table column
      >Name:
       </span> " .
       htmlentities($curbook[0]) .   //sets first position of the first data element from the array of the text file
       "<br /> ";
   echo "<span
      >Book:   //second position of the first data element
       </span> " .
       htmlentities($currbook[1]) .
       "<br /> ";
   echo "<span
      >Email:   //third position of the first data element
       </span> " .
       htmlentities($currbook[3]) .
       "<br /> ";
   echo "</tr> ";      
}
   echo "</table> ";  
}
?>
<p>
<a href="postreview.php">   <!hyperlink to post a new review!>
   Post New Review</a><br />
</p>

</body>
</html>

Explanation / Answer

Parse error: syntax error, unexpected T_VARIABLE in CODE on Line 18 Error parsing CODE