gpt4 book ai didi

php - 根据数据库 pdo 检查用户名

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:31 25 4
gpt4 key购买 nike

该脚本可以将用户数据添加到数据库中,但是我想检查用户名是否正在使用,但一直遇到此错误

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 );

?>

最佳答案

这一行:

$check = "SELECT username FROM use WHERE username = '$usercheck'";

错了。 use 是 SQL 保留字。我假设您指的是 user。使用简单的变量替换将值注入(inject)查询字符串也是一个糟糕的想法。 PDO 的重点是使用参数化查询:http://us1.php.net/pdo.prepared-statements

关于php - 根据数据库 pdo 检查用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21100378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com