我有一个脚本可以验证数据并将数据提交给MySQL数据库,但是只要数据库被填充,只要在新选项卡中刷新或重新访问索引页面。填充的数据是BLANK。我想停止这种自动重新提交数据,但不知道如何做到这一点。
这是我的脚本,用于验证数据并将数据提交给数据库。
<?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页面时,都会自动添加一个新的BLANK DATABASE。我怎么能阻止这个?请帮帮我。
答案 0 :(得分:0)
$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.";
这应该在if语句
中答案 1 :(得分:0)
更改
if ($_SERVER["REQUEST_METHOD"] == "POST") {
到
if(isset($_POST['submit'])){