Mehur Mehur - 1 month ago 6
MySQL Question

How to check email address or username is valid or exist through same post?

How to check email address or username is valid or exist through same post?

HTML

<form action="/index" method="post">
<input name="post" type="text" value="Username or Email" />
<input name="submit" name="send" value="Submit" />
</form>


PHP

public function index($post)
{
$results = $this->db->where('username', $post)
->where('email', $post)
->get('users');

if($results->num_rows() > 0)
{
//The user has an email or username
echo "valid";
} else {
echo "Invalid";
}
}

Answer

Change the second ->where clause to ->or_where

public function index($post) 
    {
        $results = $this->db->where('username', $post)
                            ->or_where('email', $post)
                            ->get('users');

        if($results->num_rows() > 0)
        {
          //The user has an email or username
          echo "valid";
        } else {
          echo "Invalid";
        }
    }

Take a look at this document for reference Active Record : CodeIgniter

Comments