Announcement

Collapse
No announcement yet.

Writing to DB Master and Detail tables. Is this ok?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing to DB Master and Detail tables. Is this ok?

    Is it safe for me to use the LAST_INSERT_ID() function of mysql when writing to a 2nd table?

    Or do I need to use a transaction or read that into my own variable that I use ?

    I am just wondering if LAST_INSER_ID() could possibly pick up a value from another user if running the same application at the same time?

    Thanks for any input!

    PHP Code:
    // Pick Master
    $insert_table  'PickHeader';     
    $insert_fields = array(  
         
    'SONum' => $rs[$x][0],
         
    'PickBatchID' => "'1'",
     );
    // Insert record
    $insert_sql 'INSERT INTO ' $insert_table
        
    ' ('   implode(', 'array_keys($insert_fields))   . ')'
        
    ' VALUES ('    implode(', 'array_values($insert_fields)) . ')';
    sc_exec_sql($insert_sql);

    // Pick Details    
    $insert_table  'PickDetail';    
    $insert_fields = array(  
         
    'PickID' => "LAST_INSERT_ID()",  // <--  IS THIS OK?
         
    'ItemCode' => "'".{ItemCode}."'",
            
    'Qty' => $rs[$x][2],
            
    'BinLocation' => "'".{BinLocation}."'",
         
    'User' => "'USR'",
         
    'Warehouse' => "'000'",
     );

    // Insert record
    $insert_sql 'INSERT INTO ' $insert_table
        
    ' ('   implode(', 'array_keys($insert_fields))   . ')'
        
    ' VALUES ('    implode(', 'array_values($insert_fields)) . ')';

    sc_exec_sql($insert_sql); 

  • #2
    LAST_INSERT_ID() gives you the last autogenerated id on the connection you execute it on, it does not return the last insert id globally produced by the MySQL server.
    check that question first: Inserting an automatically generated index from a table into another table I got that this function "LAST_INSERT_ID()" do the required the question is: I'm using that in ...

    Comment


    • #3
      Thanks alvagar

      Comment


      • #4
        Ok I know this is months later now... but how would I get access to that value back in PHP again? The database knows the last inserted ID... how can I get that back in my PHP event so I can use it?

        Comment


        • #5
          You can use a SC macro for that. Maybe: sc_lookup(rs, "SELECT LAST_INSERT_ID()");

          Comment

          Working...
          X