该脚本可以将用户数据添加到数据库中,但我想检查用户名是否正在使用但是仍然遇到此错误
Fatal error: Call to a member function rowcount() on a non-object in /home/4507408/public_html/registeruser.php on line 78
我似乎无法用PDO做到这一点,任何帮助都会很棒!
<?php
$form = $_POST;
$username = $form[ 'username' ];
$password = $form[ 'password' ];
$firstname = $form[ 'firstname' ];
$location = $form[ 'location' ];
$age = $form[ 'age' ];
$email = $form[ 'email' ];
$usercheck = $_POST['username'];
$check = "SELECT username FROM use WHERE username = '$usercheck'";
$query = $DBH->prepare( $check );
$query = $DBH->prepare($check);
$query->execute();
$data = $query->fetchALL();
$check2 = $check->rowcount();
//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}
$sql = "INSERT INTO user ( username, password, firstname, location, age, email ) VALUES ( :username, :password, :firstname, :location, :age, :email )";
$query = $DBH->prepare( $sql );
?>
答案 0 :(得分:4)
这一行:
$check = "SELECT username FROM use WHERE username = '$usercheck'";
错了。 use
是SQL保留字。我假设你的意思是user
。使用简单的变量替换将值注入查询字符串也是一个可怕的想法。 PDO的重点是使用参数化查询:http://us1.php.net/pdo.prepared-statements