« Heather Flies | Main | Isaac Newton - Criminal Investigator »
Thursday
Sep032009

A Bug in Beginning PHP6, Apache, MySQL Web Development

I have been working my way through Beginning PHP6, Apache, MySQL Web Development by Timothy Boronczyk et al (Wiley, 2009), carefully doing all the exercises and examples.  In Chapter 6 (Letting the user Edit the Database), on pages 157-159, there is a listing called movie.php which, among other things, displays 3 drop-down selection boxes.  When I ran the program, I noticed that each option in these selection boxes appeared twice. At first I thought I had made an error in typing in the code, but no, I had typed everything in correctly.  Here is the loop that constructs the <option> tags for the first selection box:

while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $value) {
        echo '<option value="' . $row['movietype_id'] . '">';
        echo $row['movietype_label'] . '</option>';
    }
}

The clue lies in the fact that the foreach loop declares the name $value but $value is not referred to anywhere in the loop body. The loop body accesses the elements of $row directly through their indexes. The foreach loop is not needed; all it does is cause the option to be duplicated. This is what the code should be:

while ($row = mysql_fetch_assoc($result)) {
    echo '<option value="' . $row['movietype_id'] . '">';
    echo $row['movietype_label'] . '</option>';
}

The same correction should be made to the two other selection boxes in movie.php. It also needs to be made to the version of movie.php on pages 167-170.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.