当我刷新浏览器时,每次按REFRESH时,注册表的条目都会进入数据库,教授告诉我在LAST_INSERT_ID()
的帮助下解决这个问题。
我可以从数据库中获取last_insert_id
,但我不知道如何使用该ID进一步操作。
请帮忙..
enter image description here
答案 0 :(得分:0)
推荐的方法是使用Post/Redirect/Get模式。 还有其他方法可以达到您的目标here。
我不确定你的教授要求对最后一个插入ID做什么。也许他指的是这样的东西,
if(isset($_SESSION['last_insert_id'])){ // At the beggining
//redirect to a another location
}
// Code for insertion goes here
$_SESSION['last_insert_id'] = $last_insert_id; // Get and store the insertion id as a session
答案 1 :(得分:0)
我认为您使用相同的页面来保存数据,如果是,那么请按照以下方法:
<?php
if(isset($_POST[userName]))
{
// Put your Registration Operation Code Here
header('Location: ./Registrationform.php');
}
?>
DataBase插入后,它会重定向到同一页面。现在使用GET方法而不是POST方法进行刷新。因此,您可以通过这种方式消除重复的条目。
答案 2 :(得分:0)
根据您的要求,我在数据库中插入记录之前使用了最后插入的ID进行验证。
<?php
session_start();
if(isset($_POST[userID]))
{
$flag = false;
if(isset($_SESSION['last_insert_id']))
{
if($_SESSION['last_insert_id'] == $_POST[userID])
{
$flag = false;
header('Location: ./Registrationform.php');
}
else
{
$flag = true;
$_SESSION['last_insert_id'] = $_POST[userID];
}
}
if($flag == true)
{
// Put your Registration Operation Code Here
}
}
?>