View Full Version : problem with doing two queries inside of the same if statement

06-09-2005, 05:20 AM
Basically what I am doing is inserting a new row into the table, then once it is inserted i need to grab the gunManufacturerID that was just inserted into that field since it is on auto_increment. But when I try and echo out the {$row2['gunManufacturerID']} i get the error: Parse error: parse error, unexpected T_ECHO in /home/www/midevilsports/public_html/site_test/add_gun_info.php on line 62
Line 62 is the line that the echo is on.

if($newGunManufacturer) {
$query = "INSERT INTO gun_manufacturers VALUES ('','{$newGunManufacturer}')";
$result = mysql_query($query,$conn);
$query2 = "SELECT gunManufacturerID FROM gun_manufacturers WHERE gunManufacturerName = '{$newGunManufacturer}'";
$result2 = mysql_query($query2,$conn);
$row2 = mysql_fetch_array($result2, MYSQL_ASSOC)
echo "{$row2['gunManufacturerID']}";

06-09-2005, 05:31 AM
Your missing a semi colon here,
$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);

but, you can simply use mysql_insert_id() (http://www.php.net/manual/en/function.mysql-insert-id.php) to get the last id.

Kid Charming
06-09-2005, 05:33 AM
You're missing the semicolon from the line above.

The setup you're using is unnecessary, though. You can get the last value of an autoincremented column with PHP's mysql_insert_id() (http://us2.php.net/manual/en/function.mysql-insert-id.php) or MySQL's LAST_INSERT_ID() (http://dev.mysql.com/doc/mysql/en/information-functions.html) function. They're slightly different, so read up on them and decide which meets your needs best (often PHP's function is better because you don't have to make another query for it).

[Edit]Like he said. :)

06-09-2005, 05:59 AM
oh man thank's a bunch guys