$sql = INSERT INTO char_lair (id, zip_id, lair_addr) (Sex offenders web site)

$sql = INSERT INTO char_lair (id, zip_id, lair_addr) . VALUES (NULL, $zip , $address ) ; $result = mysql_query($sql) or die(mysql_error()); You also could have left out the id field from the insert, and inserted values into the zip_id and lair_addr columns only. MySQL treats ignored columns as if you had attempted to insert NULL into them. We like to specify every column when doing an insert, though. If you needed to modify your SQL statement later, having all the columns in the INSERT query gives you a nice placeholder so all you have to do is modify the inserted value. The following is a neat little function. Assuming the insert worked properly ($result returned TRUE), the mysql_insert_id() function will return the value of the last auto_increment from the last run query. This works only after running a query on a table with an auto_incremented column. In this case it returns the primary key value of the row you just inserted into the char_lair table. You will need that value to insert into the char_main table. if ($result) { $lairid = mysql_insert_id($conn) } The connection variable is optional, but we think it s a good habit to always include it. If you omit it, it will use the most recently opened connection. In a simple application like this one, that s not a problem; in a more complex application where you might have more than one connection, it could get confusing. Again, note the use of NULL for the primary key id, and the use of mysql_insert_id() to return the primary key in the following: $sql = INSERT INTO char_main (id, lair_id, alias, real_name, align) . VALUES (NULL, $lairid , $alias , $name , $align ) ; $result = mysql_query($sql) or die(mysql_error()); if ($result) { $charid = mysql_insert_id($conn) } You are always interested in minimizing the number of times you run a query on the database. Each hit takes precious time, which can be noticeable in a more complex application. At this point, you need to figure out how to insert all the powers with only one SQL command: if ($powers != ) { $val = ; foreach ($powers as $key => $id) { $val[] = ( $charid , $id ) ; } $values = implode( , , $val); $sql = INSERT IGNORE INTO char_power_link (char_id, power_id) . VALUES $values ; $result = mysql_query($sql) or die(mysql_error()); } 296 Chapter 10
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.