【问题标题】:Checking for duplicate mysql data with bootstrap validator使用引导验证器检查重复的 mysql 数据
【发布时间】:2019-01-10 06:36:04
【问题描述】:

我使用引导验证程序创建了一个注册表单并将其连接到 MySQL 数据库。我希望通过检查现有电子邮件数据并通过引导验证器脚本返回消息来防止重复条目。我正在使用远程方法,但问题是,一旦我开始在电子邮件字段中输入,我就会收到“电子邮件已被占用”。消息,尽管电子邮件可能不存在于数据库中,或者仅输入了一个字母。即使我没有提交表格,我也会收到数据条目。我在网上搜索并意识到大多数解决方案都存在于 jquery 验证插件但没有引导验证器。我需要帮助,请。这是我的代码:

signup.php

 <div class="signup-form">
 <h1>Sign up for free!</h1><br>

 <form id="form1" action="registration.php" class="loading-form" method="POST">

<div class="form-group">
  <label for="name-input">Username</label>
  <input required type="text" name="username" class="form-control" id="username" maxlength="100">
</div>    

  <label for="email-input">Email</label>
  <input required name="email" type="email" class="form-control" id="email" title="An email is required">
</div>

<p>You accept our <a href="pages/terms.php" style="color: #337ab7;">Terms &amp; Conditions</a> by creating your account.</p>

<div class="form-group">
<!-- Do NOT use name="submit" or id="submit" for the Submit button -->
<button type="submit" class="btn btn-success">Sign up</button>
</div>

  <script>
  $(document).ready(function() {
  $('#form1').bootstrapValidator({
    // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        username: {
            message: 'The username is not valid',
            validators: {
                notEmpty: {
                    message: 'The username is required'
                },
                stringLength: {
                    min: 6,
                    max: 30,
                    message: 'The username must be more than 6 and less than 30 
        characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z-' ]+$/,
                    message: 'The username can only consist of alphabetical and number'
                },
                different: {
                    field: 'password',
                    message: 'The username and password cannot be the same as each other'
                }
            }
        },
        email: {
            validators: {
                notEmpty: {
                    message: 'The email address is required'
                },
                emailAddress: {
                    message: 'The email address is not a valid'
                },
                remote: {
                    message: 'The email address is already taken.',
                    url: "registration.php"
                }
            }
        },
    }
});
});

registration.php

<?php   
include ("connect.php");
session_start();

$username = $_POST['username'];
$email = $_POST['email'];

$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$query1 = "SELECT * FROM registration where (email='$email')";
$check = mysqli_query($con, $query1);
$checkrows=mysqli_num_rows($check);

if($checkrows>0) {
echo json_encode(FALSE);
}
else
{
echo json_encode(TRUE);
}

//insert results from the form input
$query = "INSERT INTO registration (username, email) VALUES('$username', '$email')";
$result = mysqli_query($con, $query);
$num1=mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);

?>

【问题讨论】:

    标签: php html mysql bootstrap-4


    【解决方案1】:

    在“registration.php”文件的末尾,您将收到的信息插入到数据库中。

    这意味着您插入的是重复的,甚至是格式错误的电子邮件地址。此外,如果您的 javascript 尝试在每次按键时进行验证,那么您将插入每个字母。例如,如果您正在注册 test@example.com,则您有一个使用电子邮件“t”的注册,另一个使用“te”,依此类推,直到完成电子邮件。

    可能,这就是问题所在。

    你需要做一些修改: 首先:对接收到的值进行一些检查。例如,如果 checkrows > 0,则永远不要执行插入查询。还要检查电子邮件的格式是否正确,在 PHP 方面。

    另外,尝试仅在用户单击提交时才写入表。

    您可以使用两个文件:一个用于检查,另一个用于验证并将结果写入表。

    【讨论】:

      【解决方案2】:

      首先,当表中存在电子邮件地址时,您的 php 文件永远不会停止脚本。因为你的 if 语句有问题。

      <?php
      header('Content-type: application/json');
      
      $valid = true;
      $message = '';
      
      include ("connect.php");
      session_start();
      
      $username = $_POST['username'];
      $email = $_POST['email'];
      
      $username = mysqli_real_escape_string($con, $_POST['username']);
      $email = mysqli_real_escape_string($con, $_POST['email']);
      
      $query1 = "SELECT * FROM registration where (email='$email')";
      $check = mysqli_query($con, $query1);
      $checkrows = mysqli_num_rows($check);
      
      if($checkrows > 0) {
         // If the email address exists in the table, the ``$valid`` variable define as false.
         $valid = false;
         $message = 'The email address exists.';
      }
      else
      {
         // If the email address has not exists in the table, adds new record to database and $valid variable returns as true.
         //insert results from the form input
         $query = "INSERT INTO registration (username, email) VALUES('$username', '$email')";
         $result = mysqli_query($con, $query);
         $num1=mysqli_num_rows($result);
         $row = mysqli_fetch_assoc($result);
      }
      
      echo json_encode(
      $valid ? array('valid' => $valid) : array('valid' => $valid, 'message' =>     $message)
      );
      
      ?>
      

      请查看官方 repo 中的 remote.php 演示文件。 https://github.com/nghuuphuoc/bootstrapvalidator/blob/master/demo/remote.php

      【讨论】:

      • 您的脚本很有帮助,但我对其进行了一些修改。我取出插入查询并将其放在单独的 php 页面中。现在可以了!
      【解决方案3】:

      这是对我有用的代码:

      注册

      header('Content-type: application/json');
      
      $valid = true;
      $message = '';
      
      ob_start();
      error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
      
      include ("connect.php");
      session_start();
      
      //get the name and comment entered by user
      $fullName = $_POST['fullName'];
      $userName = $_POST['userName'];
      $birthday = $_POST['birthday'];
      $gender = $_POST['gender'];
      $email = $_POST['email'];
      $password = $_POST['password'];
      
      
      $fullName = mysqli_real_escape_string($con, $_POST['fullName']);
      $userName = mysqli_real_escape_string($con, $_POST['userName']);
      $birthday = mysqli_real_escape_string($con, $_POST['birthday']);
      $email = mysqli_real_escape_string($con, $_POST['email']);
      $password = mysqli_real_escape_string($con, $_POST['password']);
      
      //insert results from the form input
      $query1 = "SELECT * FROM registration where (email='$email')";
      $check = mysqli_query($con, $query1);
      $checkrows=mysqli_num_rows($check);
      
      if($checkrows > 0) {
         // If the email address exists in the table, the ``$valid`` variable define as false.
         $valid = false;
         $message = 'The email address exists.';
      }
      else
      {
      
      }
      
      echo json_encode(
      $valid ? array('valid' => $valid) : array('valid' => $valid, 'message' =>     $message)
      );
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-19
        • 2019-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 2021-11-12
        • 1970-01-01
        相关资源
        最近更新 更多