php oop check if user exists?

Viewed 1791

I want to display an error if a username exists, however no error is being thrown.

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

i referenced this, however it is not relevant to 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 Answers
public function ifUserAlreadyExist(string $email):bool{
    $sql = "SELECT 1 FROM users WHERE email= :Email";
    $statment = $this->conn->prepare($sql);
    if (false === $statment) {
        return false;
    }
    $statment->execute([':Email' => $email]);
    return (bool)$statment->fetchColumn();
}

//You need to just select 1 object if is already exist and in this case function hint will be so handy, can set the function to boolean and see if it return true or false. I hope I could help.

Related