我使用引导验证器创建了一个注册表单并将其连接到 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 & 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);
?>
最佳答案
在“registration.php”文件的末尾,您将在数据库中插入收到的信息。
这意味着您插入了重复的电子邮件地址,甚至是格式错误的电子邮件地址。另外,如果您的 JavaScript 尝试在每次按键时进行验证,则您将插入每个字母。例如,如果您要注册 test@example.com,则您有一个使用电子邮件“t”的注册,另一个使用“te”的注册,依此类推,直到完成电子邮件。
可能这就是问题所在。
您需要做一些修改: 首先:对接收到的值进行一些检查。例如,如果 checkrows > 0,则永远不执行插入查询。还要检查 PHP 端的电子邮件格式是否正确。
此外,仅当用户单击“提交”时才尝试写入表。
您可以使用两个文件:一个用于检查,另一个用于验证并将结果写入表。
关于php - 使用引导验证器检查重复的 mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53712087/