Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

last_insert_id 1

Status
Not open for further replies.
Jul 28, 2005
358
FR
I am getting very confused trying to get last_insert_id to work.

I have two tables
Country
with fields: cid (auto increment primary index)
country_name (varchar 40)

Region
with fields: rid (auto increment primary index)
cid (INT)
region_name (varchar 40)

I need to insert a country into the country table then insert a region into the region table aith cid being the same value of the newly created cid inthe country table.

But, everytime I run this it just gives cid in the region table a value of 0, not the new index value that was created in cid.

How can I get this value to insert.

My php/mysql code is below

Code:
$countrysql="insert into country (country_name) values ('$newcountry') ";
$result= @mysql_query($countrysql) or die(mysql_error());
$country=$newcountry;
$cidsql="select LAST_INSERT_ID() from country";
$cid=mysql_query($cidsql) ;
$regionsql="insert into region (cid, region_name) values ('$cid', '$newregion')";
$result=mysql_query($regionsql);

Thanks,

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top