php - 页面刷新时数据库被填充

标签 php jquery html mysql forms

我有一个脚本,用于验证数据并将数据提交到 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&nbsp;&nbsp;&nbsp;</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&nbsp;&nbsp;&nbsp;</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&nbsp;&nbsp;&nbsp;</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&nbsp;&nbsp;&nbsp;</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 = "INSERT INTO user_profile (姓名、城市、联系人、服务)VALUES('$name', '$city','$contact', '$service')"; mysql_query($query) 或 die(mysql_error()); $myDb->close(); $display_error = "数据提交成功。";

这应该在 if 语句内

关于php - 页面刷新时数据库被填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581396/

相关文章:

javascript - 如果元素不适合 A4 尺寸,请为其留出空间

javascript - 使用 href 将文本字段值发送到 php

php - 访问对象变量上的关系会更改变量

带有 <? ?> XAMPP 中的标签

jquery - 如何在 ASP.NET MVC 操作中注册 javascript?

html - Chrome 中带有负 translateZ 的悬停元素

javascript - 如何使用jquery检测文本以特殊单词开头的元素数量?

php - 如何仅显示数据库中表中的某些行?

php - 如何在 php 中使用 bind_result() 而不是 get_result()

javascript - 带有可排序 UI 的 jQuery 拖放不起作用