Chapter 10 code errors in the book
I have finished the Comic Book Appreciation exercise of Chapter 10 and have found a few mistakes in the code in the book:
#1
In charlist.php the code to select the character-enemies (good-bad or bad-good) combinations in order to display them on the home page is as follows:
$sql = "SELECT c.id, n.alias " .
"FROM char_main c " .
"JOIN char_good_bad_link gb " .
"JOIN char_main n " .
"ON (c.id = gb.good_id AND n.id = gb.bad_id) " .
"ON (n.id = gb.good_id AND c.id = gb.bad_id)";
This query does not return any records from char_good_bad_links table even though there are records that shoud be returned. The home page has "none" for enemies in each row. I struggled with this using MySQLQueryBrowser to test various queries.
Fianally I found an article at "http://www.devx.com/dbzone/Article/17403/1954?pf=true" titled "Getting the Right Data with SQL Joins" by Susan Sales Harkins and Patricia E. O' Connor, This is what it said about 3 table joins"
"SELECT fldlist FROM table1 JOIN table2
ON table1.primarykey = table2.foreignkey JOIN table3
ON table2.primarykey = table3.foreignkey
The first join applies to table1 and table2, the second join applies to table2 and table3. You might hear table2 referred to as the intermediate or translation table because it's the connection table for the other two tables. In a really complex statement, you might have several intermediate tables. Continue to add joins in this manner until your statement includes all the relationships and tables necessary to get just the right results. This is another fairly generic behavior that may or may not be strictly enforced by your system; be sure to check your system's documentation." end of quote.
Based on this syntax I found that the following worked:
$sql = "SELECT c.id, n.alias " .
"FROM char_main c " .
"JOIN char_good_bad_link gb " .
"ON ( c.id = gb.good_id OR c.id=gb.bad_id) " .
"AND (c.id != n.id) " .
"JOIN char_main n " .
"ON (n.id = gb.bad_id OR n.id = gb.good_id)";
#2
In char_transact.php - the Delete Character switch path ,the following did not work:
$sql = "DELETE FROM char_main, char_lair " .
"USING char_main m, char_lair l " .
"WHERE m.lair_id = l.id AND m.id = $cid";
When I dropped the table aliases it worked (I don't understand why) - see below:
$sql = "DELETE FROM char_main, char_lair " .
"USING char_main, char_lair " .
"WHERE char_main.lair_id = char_lair.id " .
"AND char_main.id = $cid";
#3
In charedit.php only one character was shown in the Enemies list on the Character Edit form regardless of the number of characters in the char_main table and the number of combinations in the char_good_bad_links table This problem also appeared in a post to this forum as a question by author Sagh on 7/18/2005 whch was never replied to.
$sql = "SELECT id, alias FROM char_main WHERE id != $char";
$result = mysql_query($sql)
or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$charlist[$row['id']] = $row['alias'];
}
The problem is that this fetches only the first row of the returned records. The corrected code which is similar to the $pwrlist code is as follows:
if (mysql_num_rows($result) > 0) {
While($row = mysql_fetch_array($result)) {
$charlist[$row['id']] = $row['alias'];
}
}
#4
The code to build the character list that is used to select enemies is as follows:
<?php if (isset($charlist) && is_array($charlist)) { ?>
<tr>
<td>Enemies:<br>
(Ctrl-click to<br>select multiple<br>enemies)
</td>
<td>
<select multiple name="enemies[]" size="4">
<?php
foreach ($charlist as $key => $value) {
echo "<option value=\"$key\" ";
if (isset($enemies)) {
echo $enemies[$key];
}
echo ">$value</option>\n";
}
?>
When the user comes back to charedit.php to change something the above foreach loop gets an undefined item error on each character not selected as an enemy. The foreach loop should be: (this is similar to the foreach power loop)
foreach ($charlist as $key => $value) {
echo "<option value=\"$key\" ";
if (isset($enemies) && array_key_exists($key,$enemies)) {
echo $enemies[$key];
}
echo ">$value</option>\n";
}
I don't know wheather or not these errors are corrected in an Errata list. Maybe the authors left these errors in as an
exercise for the student to find and fix. Maybe these clues will help other newbies.
Regards,
Al
|