插入新行(寄存器),同时在另一列中插入“ Auto Increament ID”

时间:2019-04-18 05:14:15

标签: php mysqli

插入新行(寄存器),同时在另一列中插入“自动增量ID”

我不知道该怎么做(可以进行正常注册)

$query = "insert into usertable (name,email,password) values('$username','$email','$password')";

在数据库表中

ID(AI)   name    email          password   nameID
1      test1     test1@gmail.com    1234   NULL(What i want: 1) 
2     test2      test2@gmail.com    1234   NULL(What i want: 2)
3     test3      test3@gmail.com    1234    NULL(What i want: 3)

1 个答案:

答案 0 :(得分:0)

是的。您可以使用最后插入的ID mysqli_insert_id($conn)

$query = "insert into usertable (name,email,password) values('$username','$email','$password')";
        if (mysqli_query($conn, $query)) { //  after you insert executed successfully
            $last_id = mysqli_insert_id($conn); //your last inserted auto increment value
            //update with same row another column
            mysqli_query($conn,"UPDATE usertable SET nameID='$last_id' WHERE ID='$last_id'");
           }