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!

How to use AutoIncrement: ID from one table in another

Status
Not open for further replies.

34534534534555

IS-IT--Management
Jan 8, 2003
240
GB
Hi

I am inserting a record into table: Auction, which has an auto increment field -auctionid. I then want to use this auction.auctionid in the table: Bid, how can i do this?This is my code, and what i want to do!!

Code:
$query ="INSERT INTO auction
				(sellerusername, isbn, status, bookcondition, buyitnow)
			VALUES
				('$username', '$isbn', 'open', '$bookcondition', '$buyitnowprice')";
	
	$query1 ="INSERT INTO bid
				(auctionid, bidprice, bidusername)
			 VALUES
				(LAST_INSERT_ID(auctionid), '$startprice', '$username')";
	
	$sql = mysql_query($query) or die("Invalid query: " . mysql_error()); 
	$sql = mysql_query($query1) or die("Invalid query: " . mysql_error());


TIA


| Feedback is always appreciated as this will help to further our knowledge as well |
 
Code:
$query ="INSERT INTO auction
                (sellerusername, isbn, status, bookcondition, buyitnow)
            VALUES
                ('$username', '$isbn', 'open', '$bookcondition', '$buyitnowprice')";
    	$auctionid=mysql_insert_id();
    $query1 ="INSERT INTO bid
                (auctionid, bidprice, bidusername)
             VALUES
                (LAST_INSERT_ID(auctionid), '$startprice', '$username')";
    
    $sql = mysql_query($query) or die("Invalid query: " . mysql_error()); 
    $sql = mysql_query($query1) or die("Invalid query: " . mysql_error());

with $auctionid=mysql_insert_id();
 
Keep in mind that in a multi-processing environment, auto increment does not work. Mysql gets all confused. Not sure if it has been fixed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top