[英]Check username exists in DB with jquery and mysql/php
I try since two days a check from the username befor a new user can registration. 自两天以来,我尝试使用用户名进行检查,因为新用户可以注册。 I have read all what i have found but i can't find the problem. 我已经阅读了所有发现的内容,但找不到问题。
When the user check is the name free, i not become a answer from php/msql. 当用户检查是免费的名称时,我不会成为php / msql的答案。 Can you please check my code? 你能检查我的密码吗?
Table code: 表格代码:
<form id="form" method="get" action="">
<legend>Formular mit Validierung</legend>
<tr>
<td><label id="geschlecht">Geschlecht:* </label></td>
<td><select name="anrede" value=""/>
<option> Frau </option>
<option> Mann </option>
<option> Paar </option>
</select>
</td>
</tr>
<tr>
<td><label for="vorname">Vorname* </label></td>
<td><input type="text" id="vorname" name="vorname" />
</td>
</tr>
<p>
<label for="nachname">Nachname* (mindestens 2 Zeichen)</label>
<input type="text" id="nachname" name="nachname" />
</p>
<p>
<label for="email">E-Mail*</label>
<input id="email" name="email" />
</p>
<p>
<label for="geburtsDatum">Geburtsdatum:*</label>
<input type="text" name="geburtsDatum" id="geburtsDatum" value=""/>
</p>
<p>
<label for="username">Benutzername:*</label>
<input type="text" name="username" id="benutzername" value=""/>
</p>
<p>
<label for="message">Mitteilung*</label>
<textarea id="message" name="message"></textarea>
</p
><p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
js code: js代码:
$(document).ready(function() {
// hier die Methode .validate()
$("#form").validate({
submitHandler: function() {
},
rules: {
vorname: {
required: true,
minlength: 3
},
nachname: {
required: true,
minlength: 3
},
email: {
required: true,
email: true
},
geburtsDatum: {
required: true,
date: true
},
username: {
required: true,
minlength: 3,
remote: "check.php"
},
},
messages: {
username: {
required: "Bitte Tragen Sie ihren Benutzername ein",
minlength: "Der Benutzername ist zu kurz",
remote: "Somenoe have already chosen nick like this."
},
dateformat: "Choose your preferred dateformat",
terms: " "
}
});
var newsletter = $("#newsletter");
var isChecked = newsletter.is(":checked");
var themen = $("#newsletter_themen");
themen[isChecked ? "show" : "hide"]();
//var themaInputs = themen.find("input")
// .attr("disabled", !isChecked);
newsletter.click(function() {
themen[this.checked ? "fadeIn" : "fadeOut"](1000);
//themaInputs.attr("disabled", !this.checked);
});
and the php code: 和PHP代码:
$username = trim(strtolower($_REQUEST['value']));
$sql =" SELECT login FROM benutzername WHERE login = '".$username."' LIMIT 1";
$result = $connector->query($sql);
$num = mysql_num_rows($result);
if ($num == 1){
echo "true";
I hope you can help me. 我希望你能帮助我。
Best regards Thomas 最好的问候托马斯
From what I can see there are two possibilities: 从我所看到的有两种可能性:
A) $num = mysql_num_rows($result);
A) $num = mysql_num_rows($result);
doesn't set $num
to what you think it does. 不会将$num
设置$num
您认为的那样。
B) $num != 1
: Instead of B) $num != 1
:代替
if ($num == 1) { ... }
The following will work just as well since you're testing for existence of the record, not existence of only one record 由于您正在测试记录的存在,而不是仅测试一个记录,因此以下内容同样适用
if ($num > 0) { ... }
The issue here is, if your code here is exactly the same as in your dev, $_REQUEST['value']
is wrong. 这里的问题是,如果此处的代码与开发中的代码完全相同,则$_REQUEST['value']
是错误的。
It will always be undefined because remote sends the name of the validation element
not some arbitrary placeholder. 它将始终是未定义的,因为remote sends the name of the validation element
不是任意的占位符。
You need to use $_REQUEST['username']
moreover, later on in the code, you redeclare the validation on this object. 此外,您需要使用$_REQUEST['username']
,稍后在代码中,重新声明该对象的验证。
Also........ 也........
You do echo "true".
您确实echo "true".
Which is wrong for two reasons. 这是错误的,原因有两个。 First, we need to return a json_encode
d array for this plugin to accept the response. 首先,我们需要为该插件返回一个json_encode
d数组,以接受响应。 2nd, using "true" will return "\\true\\", when we want true
. 2,当需要true
时,使用“ true”将返回“ \\ true \\”。 This is a BOOLEAN we are sending, not a string. 这是我们要发送的BOOLEAN,而不是字符串。
$username = trim(strtolower($_REQUEST['username']));
$sql = sprintf("SELECT login FROM benutzername WHERE login = '%s' LIMIT 1", $username);
$result = $connector->query($sql);
$num = mysql_num_rows($result);
if ($num == 1){
$valid = true;
}else{
$valid = false;
}
return json_encode($valid);
This should do. 这应该做。
$username = trim(strtolower($_REQUEST['value'])); SHOULD BE
$username = trim(strtolower($_REQUEST['username']));
And you should stop using mysql functions and switch to using PDO or MySQLi. 而且,您应该停止使用mysql函数,而改用PDO或MySQLi。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.