Javascript jQuery Validate Remote - 检查电子邮件是否已经存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32543598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validate Remote - Check if email already exists
提问by CyrilWalrus
I have a bit of an issue getting jQuery Validation to check if an email address already exists in a mysql table.
我在使用 jQuery 验证来检查电子邮件地址是否已存在于 mysql 表中时遇到了一些问题。
Every time I submit the form, it tells me the email already exists, even though I know it doesn't.
每次我提交表单时,它都会告诉我电子邮件已经存在,即使我知道它不存在。
Here's the code I have:
这是我的代码:
validation.js
验证.js
$(document).ready(function () {
$('#signup').validate({
errorLabelContainer: "#cs-error-note",
wrapper: "li",
rules: {
email: {
required: true,
email: true,
remote: {
url: "check-username.php",
type: "post"
}
}
},
messages: {
email: {
required: "Please enter your email address.",
email: "Please enter a valid email address.",
remote: "Email already in use!"
}
},
submitHandler: function(form) {
form.submit();
}
});
});
check-username.php
检查用户名.php
<?php
require('../../private_html/db_connection/connection.php');
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$query = $conn->prepare("SELECT * FROM 'user_accounts' WHERE email = '" . $_POST['email'] . "'");
$query->execute();
if( $query->rowCount() > 0 ){
echo 'true';
}
else{
echo 'false';
}
?>
Your help is much appreciated!
非常感谢您的帮助!
回答by Shehary
You have to change the row count if / else condition in query
您必须更改查询中的 if / else 条件的行数
Script
脚本
<script>
$(document).ready(function () {
$('#signup').validate({
errorLabelContainer: "#cs-error-note",
wrapper: "li",
rules: {
email: {
required: true,
email: true,
remote: {
url: "check-username.php",
type: "post"
}
}
},
messages: {
email: {
required: "Please enter your email address.",
email: "Please enter a valid email address.",
remote: "Email already in use!"
}
},
submitHandler: function(form) {
form.submit();
}
});
});
</script>
HTML
HTML
<form class="form-inline" role="form" id="signup">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="email" id="email">
</div>
</form>
PHP
PHP
WarningDo not use this PHP code reason rowCount()
may not work so skip it and jump to code at bottom of answer.
警告不要使用此 PHP 代码原因rowCount()
可能不起作用,因此请跳过它并跳转到答案底部的代码。
<?php
require('../../private_html/db_connection/connection.php');
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
if(isset($_POST['email'])) {
$email = $_POST['email'];
$query = $conn->prepare("SELECT email FROM user_accounts WHERE email = '$email'");
$query->execute();
if( $query->rowCount() > 0 ){
echo 'false';
} else {
echo 'true';
}
}
?>
Edit:As @Jay Blanchard very consistent and dead sure that above code will not work
编辑:作为@Jay Blanchard 非常一致,并且确定上面的代码不起作用
rowCount() doesn't work for SELECT statements. stackoverflow.com/a/31569733/1011527
Nope, this will not work because rowCount() doesn't work for SELECT statements. You're not getting a row count at all.
Try echoing $query->rowCount() and you'll see the issue
rowCount() 不适用于 SELECT 语句。stackoverflow.com/a/31569733/1011527
不,这将不起作用,因为 rowCount() 不适用于 SELECT 语句。你根本没有得到行数。
尝试回显 $query->rowCount() 你会看到问题
and makes me wonder How the above code is working on my live serverwhen It shouldn't so I done some digging and found this;
并让我想知道上面的代码如何在我的实时服务器上运行它不应该,所以我做了一些挖掘并找到了这个;
If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rowsreturned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
如果关联 PDOStatement 执行的最后一条 SQL 语句是 SELECT 语句,则某些数据库可能会返回该语句返回的行数。但是,这种行为并不能保证适用于所有数据库,并且不应依赖于可移植应用程序。
and this
和这个
For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.
对于大多数数据库, PDOStatement::rowCount() 不返回受 SELECT 语句影响的行数。相反,使用 PDO::query() 发出一个 SELECT COUNT(*) 语句,其谓词与您预期的 SELECT 语句相同,然后使用 PDOStatement::fetchColumn() 来检索将返回的行数。然后您的应用程序可以执行正确的操作。
Source of Above statements php.net manuals
In both above statements, some databasesand For most databasesrowCount()
does workbut on other-hand
在上述两个语句中,某些数据库和For most databasesrowCount()
确实有效,但另一方面
- should not be relied on for portable applications
- use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.
- 不应该依赖于便携式应用程序
- 使用 PDOStatement::fetchColumn() 来检索将返回的行数。然后您的应用程序可以执行正确的操作。
As OP only wants the count of rows and not all the data of all the rows so can be also done like this. Credit goes to @Jay Blanchard
由于 OP 只需要行数而不是所有行的所有数据,因此也可以这样做。归功于@Jay Blanchard
Use This Code Example
使用此代码示例
made some changes in PHP, use isset
function.
在 PHP 中做了一些改动,使用isset
函数。
<?php
require('../../private_html/db_connection/connection.php');
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
if(isset($_POST['email'])) {
$email = $_POST['email'];
$query = $conn->prepare("SELECT email FROM user_accounts WHERE email = '$email'");
$query->execute();
$rows = $query->fetchAll();
$total_rows = count($rows);
if( $total_rows > 0 ){
echo 'false';
} else {
echo 'true';
}
}
?>
回答by Jay Blanchard
rowCount()
will not work in the situation. From the docs:
rowCount()
不会在这种情况下工作。从文档:
PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.
PDOStatement::rowCount() 返回受相应 PDOStatement 对象执行的最后一个 DELETE、INSERT 或 UPDATE 语句影响的行数。
Note that it doesn't return anything for a SELECT statement. To get the row count in your code you would do this:
请注意,它不会为 SELECT 语句返回任何内容。要获取代码中的行数,您可以这样做:
if(isset($_POST['email'])) {
$email = $_POST['email'];
$query = $conn->prepare("SELECT email FROM user_accounts WHERE email = ?");
$query->execute(array($email));
$rows = $query->fetchAll();
$num_rows = count($rows);
if( $num_rows > 0 ){
echo 'true - email exists';
} else {
echo 'false - email does not exist';
}
}
In order to avoid the possibility of SQL Injection AttackI use a prepared statement, sending an array (of one) containing the values to be used in the query in the execute()
statement.
为了避免SQL 注入攻击的可能性,我使用了一个准备好的语句,发送一个数组(一个),其中包含要在execute()
语句中的查询中使用的值。