如何检查电子邮件地址或用户名是否有效或通过同一帖子存在?

时间:2016-10-12 21:52:42

标签: php mysql codeigniter codeigniter-3

如何检查电子邮件地址或用户名是否有效或通过同一帖子存在?

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

2 个答案:

答案 0 :(得分:1)

将第二个->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"; } }

{{1}}

请参阅此文档以获取参考Active Record : CodeIgniter

答案 1 :(得分:0)

替换HTML
价值 - &gt;占位符

替换Codeigniter
哪里 - &gt; or_where修复并添加评论。

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