如何在按钮点击时获取最后插入的ID,并应以html格式显示

时间:2016-07-06 05:27:51

标签: php html mysql

需要从按钮单击数据库中获取最后插入的ID ...按钮单击后会有一个新页面...在此页面上我想打印最后插入+ 1表示下一个ID ...请帮助我



<a href="index.php"><button type="button" class="btn btn-warning">ADD MEMBER</button></a>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

嗨简单的mysqli内置function用于此

成功后insertion只需拨打此function

即可
if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);

}

或如评论中所述,您还可以query运行last inseted id

if (mysqli_query($conn, $sql)) {  

  $result= mysqli_query($con ,"SELECT id FROM Table_name ORDER BY id DESC LIMIT 1 ");
$row = mysqli_fetch_assoc($result);
$last_id = $row['id'];

    }

答案 1 :(得分:0)

$ result = mysqli_query(“SELECT * from tablename where id ='”。mysqli_insert_id($ conn)。“'”);

参考链接:http://php.net/manual/en/mysqli.insert-id.php