I am fairly new to PHP and MySQL but there are 2 functions that can do this.
PHP has a function called mysql_insert_id  to retreive the last ID and Mysql
also has last_insert_id() which you can use in your query. I have used
last_insert_id() with pretty good success but I would still recommend you
look at the documentation for these functions. This example for
last_insert_id() is off the top of my head.

            insert into table1 (PrimaryID,Field) would be the initial insert
            insert into table2 (PrimaryID,last_insert_id(),field) where
last_insert_id is the value for the foreign key

Hope this helps and let me know if you have any other questions.


"J. Wharton" <[EMAIL PROTECTED]> wrote in message
> I encountered something similar. Here is a snippet of code:
> $tdate=date("Y\\-m\\-d");
> $time=date("G\\:i\\:s");
> $query="insert into trackinginfo (IP_Address, username, date, Time,
> Processed, CommentFlag)
>  values ('$REMOTE_ADDR', '$username', '$tdate', '$time', 'N',
> '$commentExists')";
> mysql_query($query) or die(mysql_error());
> //RecordID is an auto incrementing primary key.
> $query="select RecordID from trackinginfo where IP_Address='$REMOTE_ADDR'
>   and username='$username'
>   and date='$tdate'
>   and Time= '$time'";
> //used those variables as they are static for the page. And yes, that is a
> bit of overkill you see there.
> $result = mysql_query($query) or die(mysql_error());
> $dummyvar = mysql_fetch_object($result);
> $sid=$dummyvar->RecordID;
> "Cj" <[EMAIL PROTECTED]> wrote in message
> > is there any way to determine the primary key of a row after it's added
> a
> > table in mysql?
> >
> > i need to do this because i have another table which requires the id
> > one table as a foreign key.
> >
> >

PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to