如何在php中插入值后获取最后一个id

时间:2017-06-21 09:01:51

标签: php mysql

如何在我的数据库中插入最后一个ID

这是我的代码:

    $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);

1 个答案:

答案 0 :(得分:0)

您可以使用mysqli_insert_id()方法:

$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