-1

How to get the last id inserted in my database

here is my code:

    $id = $_POST['c_id'];
    $var_id = $_POST['var_id'];
    $var_name = $_POST['variable_name'];
    $saveVar = "INSERT INTO ".$table_sub_var." 
    VALUES(NULL,".$var_id.",".$var_name.")";
    mysqli_query($connect,$saveVar);
Leorah Sumarong
  • 107
  • 4
  • 16

1 Answers1

0

You can use mysqli_insert_id() method:

$id = $_POST['c_id'];
    $var_id = $_POST['var_id'];
    $var_name = $_POST['variable_name'];
    $saveVar = "INSERT INTO ".$table_sub_var." 
    VALUES(NULL,".$var_id.",".$var_name.")";
    mysqli_query($connect,$saveVar);

    $last_inserted_id = mysqli_insert_id($connect); // return last inserted ID
B. Desai
  • 16,414
  • 5
  • 26
  • 47