<small id='dAQHu'></small><noframes id='dAQHu'>

        <legend id='dAQHu'><style id='dAQHu'><dir id='dAQHu'><q id='dAQHu'></q></dir></style></legend>

        • <bdo id='dAQHu'></bdo><ul id='dAQHu'></ul>
        <tfoot id='dAQHu'></tfoot>
      1. <i id='dAQHu'><tr id='dAQHu'><dt id='dAQHu'><q id='dAQHu'><span id='dAQHu'><b id='dAQHu'><form id='dAQHu'><ins id='dAQHu'></ins><ul id='dAQHu'></ul><sub id='dAQHu'></sub></form><legend id='dAQHu'></legend><bdo id='dAQHu'><pre id='dAQHu'><center id='dAQHu'></center></pre></bdo></b><th id='dAQHu'></th></span></q></dt></tr></i><div id='dAQHu'><tfoot id='dAQHu'></tfoot><dl id='dAQHu'><fieldset id='dAQHu'></fieldset></dl></div>

        检查用户名是否存在 PDO

        时间:2023-10-04
        <i id='b3Kln'><tr id='b3Kln'><dt id='b3Kln'><q id='b3Kln'><span id='b3Kln'><b id='b3Kln'><form id='b3Kln'><ins id='b3Kln'></ins><ul id='b3Kln'></ul><sub id='b3Kln'></sub></form><legend id='b3Kln'></legend><bdo id='b3Kln'><pre id='b3Kln'><center id='b3Kln'></center></pre></bdo></b><th id='b3Kln'></th></span></q></dt></tr></i><div id='b3Kln'><tfoot id='b3Kln'></tfoot><dl id='b3Kln'><fieldset id='b3Kln'></fieldset></dl></div>
        • <small id='b3Kln'></small><noframes id='b3Kln'>

              <tbody id='b3Kln'></tbody>

              <tfoot id='b3Kln'></tfoot>
            1. <legend id='b3Kln'><style id='b3Kln'><dir id='b3Kln'><q id='b3Kln'></q></dir></style></legend>
                <bdo id='b3Kln'></bdo><ul id='b3Kln'></ul>
                  本文介绍了检查用户名是否存在 PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我如何能够检查多个因素的组合而不是检查每个因素?所以基本上我使用的是 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                    1. <tfoot id='eN3TR'></tfoot>
                    2. <i id='eN3TR'><tr id='eN3TR'><dt id='eN3TR'><q id='eN3TR'><span id='eN3TR'><b id='eN3TR'><form id='eN3TR'><ins id='eN3TR'></ins><ul id='eN3TR'></ul><sub id='eN3TR'></sub></form><legend id='eN3TR'></legend><bdo id='eN3TR'><pre id='eN3TR'><center id='eN3TR'></center></pre></bdo></b><th id='eN3TR'></th></span></q></dt></tr></i><div id='eN3TR'><tfoot id='eN3TR'></tfoot><dl id='eN3TR'><fieldset id='eN3TR'></fieldset></dl></div>

                    3. <small id='eN3TR'></small><noframes id='eN3TR'>

                      <legend id='eN3TR'><style id='eN3TR'><dir id='eN3TR'><q id='eN3TR'></q></dir></style></legend>
                        <bdo id='eN3TR'></bdo><ul id='eN3TR'></ul>