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

假装没事ソ 提交于 2019-12-25 08:00:09

问题


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";
            }
        }

回答1:


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




回答2:


replace HTML
value -> placeholder

replace Codeigniter
where -> or_where fixed and add a comment.

<form action="/index" method="post">
   <input name="post" type="text" placeholder="Username or Email" />
   <input name="submit" name="send" placeholder="Submit" />
</form>
public function index($post) 
{
    $results = $this->db->where('username', $post)
                        ->or_where('email', $post) //where[or] : on the condition
                        ->get('users');

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


来源:https://stackoverflow.com/questions/40008992/how-to-check-email-address-or-username-is-valid-or-exist-through-same-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!