PHP PDO用户名可用性检查器 [英] PHP PDO Username Availability Checker
问题描述
我正在构建一个注册页面,该页面使用JQuery的验证器插件来验证表单.对于用户名,我使用了远程方法.因此,这是我的jquery + html代码:小提琴
I'm building a registration page that uses JQuery's validator plugin to validate the form. For the username, i used the remote method. So here's my jquery+html code: fiddle
这是Available.php:
And here's Available.php:
<?php
$usr = $_POST['username'];
$link = new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***');
$usr_check = $link->prepare("SELECT * FROM Conference WHERE Username = :usr");
$usr_check->bindParam(':usr', $usr);
$usr_check->execute();
if($usr_check->rowCount()>0)
echo false;
else
echo true;
?>
所以我的数据库中有一个测试帐户,用户名:user.当我尝试使用相同的用户名提交表单时,它没有显示错误提示用户名已接受",这表示php不正确.有什么想法我做错了吗?谢谢
So I have a test account in my database with the username: user. When I tried to submit my form with the same username, it didn't display the error saying "username taken" which means the php isn't correct. Any ideas where I went wrong? Thanks
在chrome的检查器打开的情况下运行该网站.输入用户名后,它会尝试获取Available.php,但显示"500 internal server error".看起来是这样的:
Ran the website with chrome's inspector open. When the user name is entered it DOES try to get Available.php but it says "500 internal server error". Here's what it looks like:
推荐答案
好了,与您聊天了一段时间之后,我们发现了以下内容:
Alright, after chatting with you for a while, here's what we found:
1- $ _POST应该为$ _GET,因为您使用的是jQuery.validate中的远程方法.
1- $_POST should be $_GET, since you're using a remote method from jQuery.validate.
2-您的代码在$ link而不是$ usr_check上调用bindParam(),这在您的问题中是正确的.
2- Your code was calling bindParam() on $link instead of $usr_check, which is correct in your question.
另一件事,您的代码还需要打印出真假字符串.用引号将它们引起来.
Another thing, your code also needs to print out strings for true and false. Wrap those in quotes.
这篇关于PHP PDO用户名可用性检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!