You need to pass link identifier parameter to mysql_insert_id(). It's safe to use it but there is an exception - if you use bigint as your type for auto_increment column. If the last inserted id has larger value than the int type can hold, the value you'll get will be incorrect. Instead, you should use internal MySQLs' query for obtaining last inserted id:
$query = mysql_query("SELECT LAST_INSERT_ID() as last_id");
I would add (from the MySQL manual regarding LAST_INSERT_ID()):
"The ID generated is maintained in the server on a per-connection basis", so you don't have to worry about simultaneous connections. The LAST_INSERT_ID() gives you the ID you want for the last INSERT on your connection.