데이터를 유효성을 검사하여 MySQL 데이터베이스에 제출하지만 데이터베이스가 채워지는 경우 색인 페이지가 새로 고쳐 지거나 다시 열릴 때마다 스크립트가 생깁니다. 채워진 데이터는 공백입니다. 이 자동화 된 데이터 재 제출을 중단하되이를 수행하는 방법을 모르겠습니다.데이터베이스가 새로 고침 될 때 데이터베이스가 채워짐
이것은 데이터를 유효성을 검사하여 데이터베이스에 제출하는 스크립트입니다.
<?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가 자동으로 추가됩니다. 이 문제를 어떻게 방지 할 수 있습니까? 도와주세요.
내부를해야 하는가? – Kanishk
이 문은 요청이 POST인지 확인하는 if 문 안에 있어야하며이 코드를 실행하기 전에 오류가 있는지 확인하십시오. –