richcleverley
MIS
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
Thanks,
Richard
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