当我刷新浏览器时,每次按refresh时,注册表项都会进入数据库,教授告诉我要在LAST_INSERT_ID()
的帮助下解决这个问题。
我可以从数据库中获取last_insert_id
,但不知道该如何进一步处理该ID。请帮忙。。在此处输入图像描述
建议使用Post/Rerect/Get模式。这里还有其他方法可以实现你的愿望。
我不确定你的教授要求对最后一个插入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
我认为您使用的是相同的页面来保存数据,如果是这样,则按照以下方法:
<?php
if(isset($_POST[userName]))
{
// Put your Registration Operation Code Here
header('Location: ./Registrationform.php');
}
?>
插入数据库后,它重定向到同一页。现在Refresh是用GET方法而不是POST方法进行的。因此,您可以通过这种方式消除重复条目。
根据您的要求,在将记录插入数据库之前,我使用了上次插入的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
}
}
?>