I have 2 pages, on the first page, data is collected through a formwhich goes to a database and the user is automatically forwarded to the second page. The table uses a auto increment id field. I need to now the ID of the inserted record on the second page.

I know the function is mysql_insert_id(), but when I try and implement it, it only returns 0.

Does this code have to be on the first page or the second? If it is included as part of the select statement on the first page (as I have seen done) how do I then pass the ID to the second page?

    Use mysql_insert_id() just after your insert query.

    There are a number of ways to pass this value to the next page.
    You could use the query string, i.e. $_GET, or if you're already using sessions, that might be an alternative.

    EDIT:
    if you're passing through say, a meta refresh/hyperlink, the query string should be viable.

      If the form inserts the data into a database and then goes directly to the next page how can I use the Get_ variable to sent the ID number - would it have to be sent in another form?

        After your INSERT, but before the redirect, use mysql_insert_id() to get and store the last auto-increment id in a variable. Best would be a SESSION variable. It would then be available on the second page after using session_start() to gain access to the current session.

          Write a Reply...