Php oop check if user exists?

I want to show an error if username exists, however no error occurs.

the function is on User.php and im trying to display the error from that function.

i refers to this one , however this is not the case for the OOP way.

User.php

public function check_user_exists($username)
{
    try{
        $stmt = $this->db->prepare("SELECT user_name FROM users WHERE user_name=:username");
        $stmt->execute(array(':username'=>$username));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $row['user_name'] == $username;

    }

    catch(PDOExeception $e)
    {
        echo $e->getMessage();
    }
}

      

Index.php

<?php
session_start();

require_once 'User.php';
$guest = new User();



if($guest->is_logged())
{
    $guest->redirect('profile');
}


if (isset($_POST['btn_signup']) ){

    $username = htmlentities($_POST['txt_username']);
    $unpass = htmlentities($_POST['txt_password']);
    $password = password_hash($unpass, PASSWORD_BCRYPT, ['cost' => 12] );
    $unemail = $_POST['txt_email'];
    $email = filter_var($unemail, FILTER_VALIDATE_EMAIL);

    $guest = new User();


    if($email == ""){
       $errors[]= "Enter a Email";
    }

    if($username == ""){
       $errors[]= "Enter a Username please";

    }

    if($password == ""){
        $errors[]= "Enter a Password";
    }



    if($guest->check_user_exists($username)){
        $errors[]= "Username Already Taken";
    }

    if($guest->signup($email,$password,$username)){
        $guest->redirect('profile');
        die('didnt redirect');       
    }

    else{
      $errors[]= "Invalid Entry";
    }
}

$title = "Home";
require_once 'layouts/header.php';


?>


    <div class="container">
        <div class="row">
            <div class="col-md-6">

            <?php
            if(isset($errors))
            {
               foreach($errors as $error)
               {
                  ?>
                  <div class="alert alert-danger">
                      <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?>
                  </div>
                  <?php
               }
            }
            else if(isset($_GET['joined']))
            {
                 ?>
                 <div class="alert alert-info">
                      <i class="glyphicon glyphicon-log-in"></i> &nbsp; Successfully registered <a href='index.php'>login</a> here
                 </div>
                 <?php
            }
            ?>

                <h1>Sign Up</h1>




                <form action ="" method="POST">
                  <div class="form-group">
                    <label for="Email">Email address</label>
                    <input type="email" class="form-control" aria-describedby="emailHelp" name="txt_email" placeholder="Enter email">
                  </div>

                  <div class="form-group">
                    <label for="Username">Username</label>
                    <input type="text" class="form-control" aria-describedby="emailHelp" name="txt_username" placeholder="Enter Username">
                  </div>


                  <div class="form-group">
                    <label for="Password">Password</label>
                    <input type="password" class="form-control" aria-describedby="emailHelp" name="txt_password" placeholder="Enter password">
                  </div>


                     <button type="submit" name="btn_signup" class="btn btn-primary">Submit</button>
                </form>

            </div>
        </div>
    </div>


</body>
</html>

      

+3


source to share


2 answers


public function check_user_exists($username)
{
    try{
        $stmt = $this->db->prepare("SELECT user_name FROM users WHERE user_name=:username");
        $stmt->execute(array(':username'=>$username));
        return $stmt->fetchColumn() > 0;  // fetchColumn return the number of rows selected
    }

    catch(PDOExeception $e)
    {
        echo $e->getMessage();
    }
}

      



+1


source


Your function doesn't actually return or does anything. Return the result fetch()

if it returns true - the result was found. If it returns false, there was no string matching the username. After that, you don't need to check anything as the method fetch()

will only be correct if the result is found.

Adjusted for this, your function will look like this:

public function check_user_exists($username) {
    try{
        $stmt = $this->db->prepare("SELECT user_name FROM users WHERE user_name=:username");
        $stmt->execute(array(':username' => $username));
        return $stmt->fetch(PDO::FETCH_ASSOC);
    } catch(PDOExeception $e) {
        echo $e->getMessage();
    }
}

      



Also, it is not a good idea to output errors directly (in a test / development environment this is fine, but in a live environment you have to register it ( error_log()

).

+1


source







All Articles