Php 在页面刷新时填充数据库
我有一个脚本,可以验证数据并将其提交到MySQL数据库,但每当在新选项卡中刷新或重新访问索引页时,都会填充数据库。填充的数据为空。我想停止自动重新提交数据,但不确定如何进行 这是我的脚本,用于验证数据并将其提交到数据库Php 在页面刷新时填充数据库,php,jquery,html,mysql,forms,Php,Jquery,Html,Mysql,Forms,我有一个脚本,可以验证数据并将其提交到MySQL数据库,但每当在新选项卡中刷新或重新访问索引页时,都会填充数据库。填充的数据为空。我想停止自动重新提交数据,但不确定如何进行 这是我的脚本,用于验证数据并将其提交到数据库 <?php include('includes/config.php'); // define variables and set to empty values $nameErr = $contactErr = $cityErr = $serviceErr = "
<?php include('includes/config.php');
// define variables and set to empty values
$nameErr = $contactErr = $cityErr = $serviceErr = "";
$name = $contact = $city = $service = "";
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "* Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "* Only letters and white space allowed";
}
}
if (empty($_POST["contact"])) {
$contactErr = "* Contact is required";
} else {
$contact = test_input($_POST["contact"]);
// check if contact number is well-formed
if (!preg_match("/^[0-9+]*$/",$contact)) {
$contactErr = "* Phone number should contain only numbers";
}
}
if (empty($_POST["city"])) {
$cityErr = "* City is required";
} else {
$city = test_input($_POST["city"]);
// check if city is valid
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "* Only letters and white space allowed";
}
}
if (empty($_POST["service"])) {
$serviceErr = "* Service is required";
} else {
$service = test_input($_POST["service"]);
}
}
$myDb->connect();
$query = "INSERT INTO user_profile (name, city, contact, service)VALUES('$name', '$city', '$contact', '$service')";
mysql_query($query) or die(mysql_error());
$myDb->close();
$display_error = "Data submitted successfully.";
?>
这是我的表格
<form action="index.php" method="post">
<table style="line-height: 50px;">
<tr>
<th>Name </th>
<td><input type="text" name="name" placeholder="Your Name" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px"><span class="error"> <?php echo $nameErr;?></span></td>
</tr>
<br>
<tr>
<th>Phone </th>
<td><input type="text" name="contact" placeholder="Your Contact Number" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px"><span class="error"> <?php echo $contactErr;?></span></td>
</tr>
<tr>
<th>City </th>
<td><input type="text" name="city" placeholder="Your City Name" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px"><span class="error"> <?php echo $cityErr;?></span></td>
</tr>
<tr>
<th>Service </th>
<td><select name="service" autocomplete="off" style="height:30px; border:1px; width:300px; border-radius:5px;">
<option value="">Select your service</option>
<option value=service1>Service 1</option>
<option value=service2>Service 2</option>
<option value=service3>Service 3</option>
<option value=service4>Service 4</option>
</select><span class="error"> <?php echo $serviceErr;?></span></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" style="height: 40px; width: 140px; border-radius: 5px; margin-left: 140px;margin-top: 20px;">
</form>
我不知道我犯了什么错误。每次打开包含表单的index.php页面时,都会自动添加一个新的空白数据库。我怎样才能防止这种情况?请帮帮我。
$myDb->connect;
$query=插入用户_配置文件名称、城市、联系人、服务值“$name”、“$city”、“$contact”、“$service”;
mysql\u query$query或diemsql\u错误;
$myDb->close;
$display\u error=数据提交成功
这应该在if语句中
if ($_SERVER["REQUEST_METHOD"] == "POST") {
到
该语句应该位于if语句中,在该语句中检查请求是否为POST,并在执行该代码之前检查是否有任何错误。
if(isset($_POST['submit'])){