Announcement

Collapse
No announcement yet.

Select last_insert_id()

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

  • Select last_insert_id()

    This is my code in onAfterInsert:

    sc_lookup(rs, "SELECT LAST_INSERT_ID()");
    $new_r_id = {rs[0][0]};

    sc_commit_trans();

    // Redirect
    sc_redir(gris_application, glo_main_shift_id=$new_r_id, '_self');

    The problem is that I do not get the last insert ID, but another value. If I do not use "sc_commit_trans();", I will get my last insert ID, but the form doesn't insert...
    Last edited by Zaz; 08-09-2019, 07:44 AM.

  • #2
    I read this on stackoverflow.

    "LAST_INSERT_ID() can only tell you the ID of the most recently auto-generated ID for that entire database connection, not for each individual table, which is also why the query should only read SELECT LAST_INSERT_ID() - without specifying a table. As soon as you fire off another INSERT query on that connection, it gets overwritten. If you want the generated ID when you insert to some table, you must run SELECT LAST_INSERT_ID() immediately after doing that (or use some API function which does this for you)."

    So I found out i got the last ID of the log instead of the last ID of my table. Now I switched of the log of that form. Is there another solution without switching off the log?

    Comment


    • #3
      if this can help you here is how i insert a new id

      on validate:

      $trouver_maxi = "select max(id_utilisateurs) from utilisateurs ";
      sc_lookup(num_max ,$trouver_maxi);

      $alt_max = ({num_max[0][0]});
      $ref_ins_util = $alt_max + 1;



      $remettre_auto = "ALTER TABLE utilisateurs AUTO_INCREMENT = $ref_ins_util ";

      if(sc_btn_insert){
      sc_exec_sql ($remettre_auto);
      }

      Comment


      • #4
        Thank you for your help. I have to insert the last id in another form. I'll try {num_max[0][0]}.

        Comment

        Working...
        X