gpt4 book ai didi

php - 页面刷新时如何停止将数据重新输入数据库

转载 作者:行者123 更新时间:2023-11-30 22:07:26 25 4
gpt4 key购买 nike

朋友您好,我在此代码中遇到问题,
当我刷新网页时,数据会自动插入数据库,而页面加载时,我所做的数据不会一次又一次地插入数据库。
这是我的代码-

<?php 
require './database/databaseConnection.php';

if(isset($_POST['save_button']))
{
if(($_POST['fname']&&($_POST['lname'])))
{
$first_name=$_POST['fname'];
$last_name=$_POST['lname'];
$qry="INSERT INTO user_master(first_name,last_name) values('$first_name','$last_name')";
$result= mysql_query($qry)or die(mysql_error());
if($result){
echo 'SuccessFully saved data';
}
else{
echo 'Data Not Inserted!';
}
}
}
?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" href="bootStrap/css/bootstrap.min.css">
<link rel="stylesheet" href="bootStrap/css/bootstrap.css">
</head>
<body>
<div class="container jumbotron ">

<form action="" method="post">
<table class="table-responsive">
<div class="form-group form-inline">

<tbody>
<tr>
<td> <label for="fname" class="label-info">First Name</label></td>
<td><input class="form-control" type="text" name="fname"></td>

<td><label for="lname" class="label-info">Last Name</label></td>
<td><input class="form-control" type="text" name="lname"></td>

</tr>
</tbody>

</div>
</table>
<button type="submit" name="save_button" class="btn-success" >Save Data</button>
</form>

</div>

</body>
</html>

最佳答案

避免在页面刷新时插入数据,然后使用Ajax代码
对于
只需在您的if()中编写

if($result)
{
echo 'SuccessFully saved data';
echo '<script>window.location.href="CURRENTPAGE.php"</script>';
}

关于php - 页面刷新时如何停止将数据重新输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41169902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com