检查用户名是否存在 PDO

时间:2023-05-06
本文介绍了检查用户名是否存在 PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我如何能够检查多个因素的组合而不是检查每个因素?所以基本上我使用的是 PDO,我必须确保用户名和电子邮件是唯一的.那我该怎么做呢?我见过

How would I be able to check multiple factors combined instead of checking for each one? So basically I'm using PDO and I have to make sure that the usernames and emails are unique. So how would I do that? I've seen

if ( $sthandler->rowCount() > 0 ) {
// do something here
}  

但是有没有更好的方法来做到这一点.此外,如果没有,有人可以解释我将如何处理它.

But is there a better way to do it. Also if there isn't can someone explain how I'd work with that.

编辑这是我输入到数据库中的查询代码

EDIT Here's my query code that inputs into the database

        <?php
try {
    $handler = new PDO('mysql:host=localhost;dbname=s','root', '*');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
    exit($e->getMessage());
}

$name = $_POST['name']; 
$username = $_POST['username']; 
$email = $_POST['email'];   
$password = $_POST['password']; 
$password1 = $_POST['passwordconf'];
$ip = $_SERVER['REMOTE_ADDR'];

//Verifcation 
if (empty($name) || empty($username) || empty($email) || empty($password) || empty($password1))
    {
    echo "Complete all fields";
    }

// Password match
if ($password != $password1)
    {
    echo $passmatch = "Passwords don't match";
    }

// Email validation

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
    echo $emailvalid = "Enter a  valid email";
    }

// Password length
if (strlen($password) <= 6){
    echo $passlength = "Choose a password longer then 6 character";
}





function userExists($db, $user)
{
    $userQuery = "SELECT * FROM userinfo u WHERE u.user=:user;";
    $stmt = $db->prepare($userQuery);
    $stmt->execute(array(':user' => $user));
    return !!$stmt->fetch(PDO::FETCH_ASSOC);
}

$user = 'userName';
$exists = userExists($db, $user);
if(exists)
{
     // user exists already.
}
else
{
     // user doesn't exist already, you can savely insert him.
}
if(empty($passmatch) && empty($emailvalid) && empty($passlength)) {

//Securly insert into database
$sql = 'INSERT INTO userinfo (name ,username, email, password, ip) VALUES (:name,:username,:email,:password,:ip)';    
$query = $handler->prepare($sql);

    $query->execute(array(

    ':name' => $name,
    ':username' => $username,
    ':email' => $email,
    ':password' => $password,
    ':ip' => $ip

    ));
    }
?>

推荐答案

 <?php
//Connections
try {
    $handler = new PDO('mysql:host=localhost;dbname=s','root', '*');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
    exit($e->getMessage());
}

$name = $_POST['name']; 
$username = $_POST['username']; 
$email = $_POST['email'];   
$password = $_POST['password']; 
$password1 = $_POST['passwordconf'];
$ip = $_SERVER['REMOTE_ADDR'];


//Verifcation 
if (empty($name) || empty($username) || empty($email) || empty($password) || empty($password1)){
    $error = "Complete all fields";
}

// Password match
if ($password != $password1){
    $error = "Passwords don't match";
}

// Email validation

if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $error = "Enter a  valid email";
}

// Password length
if (strlen($password) <= 6){
    $error = "Choose a password longer then 6 character";
}

if(!isset($error)){
//no error
$sthandler = $handler->prepare("SELECT username FROM users WHERE username = :name");
$sthandler->bindParam(':name', $username);
$sthandler->execute();

if($sthandler->rowCount() > 0){
    echo "exists! cannot insert";
} else {
    //Securly insert into database
    $sql = 'INSERT INTO userinfo (name ,username, email, password, ip) VALUES (:name,:username,:email,:password,:ip)';    
    $query = $handler->prepare($sql);

    $query->execute(array(

    ':name' => $name,
    ':username' => $username,
    ':email' => $email,
    ':password' => $password,
    ':ip' => $ip

    ));
    }
}else{
    echo "error occured: ".$error;
    exit();
}

这篇关于检查用户名是否存在 PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:忽略特定的 WHERE 标准 下一篇:PHP pdo 实例作为私有静态属性

相关文章