Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

last_insert_id()

Options
  • 04-02-2009 5:46pm
    #1
    Closed Accounts Posts: 73 ✭✭


    Hi
    i am problems understanding where i should put this "last_insert_id()" because i never used it

    can you advise

    this is the insert where i need it
    [PHP]
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "DI_Update")) {
    $insertSQL = sprintf("INSERT INTO business_typemaster_info (Business_Type_id) VALUES (%s)",
    GetSQLValueString($_POST, "int"));

    mysql_select_db($database_DI_Directory, $DI_Directory);
    $Result1 = mysql_query($insertSQL, $DI_Directory) or die(mysql_error());
    }
    [/PHP]


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    last insert id is usually used when you are running two queries, one after the other, and you need the primary key id from the first query to be inserted in to the second query for example, or you need to work with the id on the insert page.

    an example would be
    SELECT LAST_INSERT_ID() AS user_id FROM users
    

    this will select the user with the id from the most recent insert query out of the users table.


  • Registered Users Posts: 197 ✭✭cracker


    You call it after you have done the insert. You use it in a new query after you have done the insert. You can put is as part of a select query like "select last_insert_id()" and then the resultset will have a single column with the id of the last autogenerated field. Or you can use it as part of a subsequent insert statement if you are creating a child row on another table, just include it as one of the fields in the insert statement.

    Dave


  • Closed Accounts Posts: 73 ✭✭Protype


    "select last_insert_id()" Or you can use it as part of a subsequent insert statement if you are creating a child row on another table, just include it as one of the fields in the insert statement.
    Dave

    This is what i need it for.
    cracker wrote: »
    "just include it as one of the fields in the insert statement."
    I have tried but no joy

    This is a dreamweaver insert. where would i put it in this. the second field is master_id, this is where i need the insert value to go

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "DI_Update")) {
    $insertSQL = sprintf("INSERT INTO business_typemaster_info (Business_Type_id) VALUES (%s)",
    GetSQLValueString($_POST, "int"));

    mysql_select_db($database_DI_Directory, $DI_Directory);
    $Result1 = mysql_query($insertSQL, $DI_Directory) or die(mysql_error());
    }


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Protype wrote: »
    "select last_insert_id()" Or you can use it as part of a subsequent insert statement if you are creating a child row on another table, just include it as one of the fields in the insert statement.
    Dave

    This is what i need it for.

    I have tried but no joy

    This is a dreamweaver insert. where would i put it in this. the second field is master_id, this is where i need the insert value to go

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "DI_Update")) {
    $insertSQL = sprintf("INSERT INTO business_typemaster_info (Business_Type_id) VALUES (%s)",
    GetSQLValueString($_POST, "int"));

    mysql_select_db($database_DI_Directory, $DI_Directory);
    $Result1 = mysql_query($insertSQL, $DI_Directory) or die(mysql_error());
    }

    i think this is what you mean:

    [php]
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "DI_Update")) {
    $insertSQL = sprintf("INSERT INTO business_typemaster_info (Business_Type_id, master_id) VALUES (%s, LAST_INSERT_ID())",
    GetSQLValueString($_POST, "int"));

    mysql_select_db($database_DI_Directory, $DI_Directory);
    $Result1 = mysql_query($insertSQL, $DI_Directory) or die(mysql_error());
    }
    [/php]


  • Closed Accounts Posts: 73 ✭✭Protype


    is that all i need to do

    [PHP](Business_Type_id, master_id) VALUES (%s, LAST_INSERT_ID())",
    [/PHP]


  • Advertisement
  • Closed Accounts Posts: 73 ✭✭Protype


    Let me ask this question

    HOW DO YOU use this " last_insert_id() " as in "where will if fit into code"

    please help


  • Registered Users Posts: 4,769 ✭✭✭cython


    Protype wrote: »
    Let me ask this question

    HOW DO YOU use this " last_insert_id() " as in "where will if fit into code"

    please help
    I think there is some confusion here over what exactly you are using the last_insert_id() function for. Can you clarify why you want to call it, and what you are looking to do with the value it returns please?


  • Closed Accounts Posts: 73 ✭✭Protype


    cython wrote: »
    I think there is some confusion here over what exactly you are using the last_insert_id() function for. Can you clarify why you want to call it, and what you are looking to do with the value it returns please?


    Adam got me sorted.

    What i needed was the id from a master table to be inserted into a mapping table.

    Thanks to adam i just got it working.

    Now i will need to start a new tread cos i have a new problem.

    SEE "Inserting multi into mapping table".

    Thanks to all for you advice and help


Advertisement