使用bootstrap验证器检查重复的mysql数据

8iwquhpp  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(363)

我用引导验证程序创建了一个注册表单,并将其连接到一个mysql数据库。我希望通过检查现有电子邮件数据并通过引导验证程序脚本返回消息来防止重复条目。我正在使用远程方法,但问题是,一旦我开始在电子邮件字段中键入,我就会收到“电子邮件已被接收”消息,尽管该电子邮件可能尚未存在于数据库中,或者仅键入了一个字母。即使我还没有提交表单,我也会得到数据条目。我在网上搜索了一下,发现大多数解决方案都是针对jquery验证插件的,但没有针对引导验证程序的。我需要帮助。这是我的密码:
注册.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"
                }
            }
        },
    }
});
});

注册.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);

?>
iq0todco

iq0todco1#

首先,当表中存在电子邮件地址时,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

7cjasjjr

7cjasjjr2#

在“registration.php”文件的末尾,您将在数据库中插入接收到的信息。
这意味着你插入了重复的,甚至是格式错误的电子邮件地址。另外,如果您的javascript尝试在每次按键时进行验证,则插入每个字母。例如,如果您正在注册test@example.com,你有一个用电子邮件“t”注册的,另一个用“te”注册的,依此类推直到完成电子邮件。
也许,这就是问题所在。
您需要做一些修改:首先:对接收到的值执行一些检查。例如,如果checkrows>0,则从不执行insert查询。还要检查电子邮件的格式是否正确,在php端。
此外,请尝试仅在用户单击submit时写入表。
您可以使用两个文件:一个用于检查,另一个用于验证并将结果写入表。

fcg9iug3

fcg9iug33#

以下是对我有效的代码:
注册

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)
);

?>

相关问题