case Delete Character : $sql = DELETE FROM char_main, (Web server application)

case Delete Character : $sql = DELETE FROM char_main, char_lair . USING char_main m, char_lair l . WHERE m.lair_id = l.id AND m.id = $cid ; $result = mysql_query($sql) or die(mysql_error()); $sql = DELETE FROM char_power_link WHERE char_id = $cid ; $result = mysql_query($sql) or die(mysql_error()); You don t really need to put the results of the mysql_query command in a variable. We like to do this as a matter of habit because if you ever need the return value later, it will be available. In the case of a DELETE, you don t get a result set, you get a return value of either TRUE or FALSE. Remembering that the char_good_bad_link needs to maintain what we call reverse referential integrity (1, 3 matches 3, 1), you remove all rows that contain the character s ID in either column: $sql = DELETE FROM char_good_bad_link . WHERE good_id = $cid OR bad_id = $cid ; $result = mysql_query($sql) or die(mysql_error()); Updating a character is where things get interesting. First of all, you can simply do an INSERT IGNORE on the ZIP code table. If the address and ZIP code change, you don t really need to delete the old data because it might be used for other characters it s perfectly fine to leave the old data alone. So, you just do an INSERT IGNORE as you did for a new character, and leave it at that. case Update Character : $sql = INSERT IGNORE INTO char_zipcode (id, city, state) . VALUES ( $zip , $city , $state ) ; $result = mysql_query($sql) or die(mysql_error()); Here is the first UPDATE query, and incidentally, the only one in the entire application. It is very similar to INSERT and SELECT queries, with the exception of the SET keyword. The SET keyword tells MySQL what columns to set, and what values to set them to. The old values in the row are overwritten. This is a JOIN query because there is more than one table. The WHERE keyword specifies both the linking column (lair_id) and the condition that only rows for this character will be updated. $sql = UPDATE char_lair l, char_main m . SET l.zip_id= $zip , l.lair_addr= $address , . alias= $alias , real_name= $name , align= $align . WHERE m.id = $cid AND m.lair_id = l.id ; $result = mysql_query($sql) or die(mysql_error()); Because the char_power_link table does not have an automatically incremented column as the primary key, you don t have to do an update to the table. An update is possible, but it is much easier to simply delete all the old links of character to power, and insert new link rows. In some cases, you may be deleting and inserting the same data (for instance, you might be adding flight as a power, but invisibility 298 Chapter 10
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.