我正在尝试将数据从PHP表单插入数据库。我已经编写了下面的代码,但是我不明白为什么每次刷新表单时都会将先前输入的数据添加到我的数据库中。 如果有人可以帮我解决这个问题,请。
<! DOCTYPE html>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php include 'config.php'; ?>
<?php
if(isset($_POST['submit'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$addr = $_POST['addr'];
$phone = $_POST['phone'];
$query = "INSERT INTO `employee`(fname,lname,addr,phone) VALUES ('$fname','$lname','$addr','$phone')";
$result = mysqli_query($con,$query) or die ("problem inserting new product into database");
}
?>
<h2>Sign Up</h2>
<h3>Enter the data to Sign Up</h3>
<p><span class="error">* required field</span></p>
<form action = "" method = "post">
Name: <input type = "text" name = "fname">
<span class=error>*</span><br>
Surname: <input type="text" name="lname">
<span class=error>*</span><br>
Address: <input type = "text" name = "addr">
<span class=error>*</span><br>
Phone Number: <input type = "tel" name="phone"><br>
<input type = "submit" name="submit" value="Submit">
</form>
</body>
</html>
答案 0 :(得分:1)
验证表单时,可以在页面的eheader中创建POST值。如果您重新加载刚刚接受过操作的页面,它将重复播放。
在
之后$result = mysqli_query($con,$query) or die ("problem inserting new product into database");
您可以添加
header('Location : nameofyourpage.php')
或
unset($_POST)