0

我正在研究“发送我的密码”类型的功能,我的 sql 不好或者我的 codeigniter 不好。表单验证得很好,模型中的函数被调用,但没有任何结果。我必须检查提交的电子邮件地址的两个表,因为我们将用户和管理员帐户分成两个表。

控制器代码:

 public function send() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim');
    $this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div><br />');

    if ($this->form_validation->run()) {
        // Check For A Matching Email
        $this->load->model('signin_model');
        if ($this->signin_model('email_verify')) {

            // There Was A Match
            echo "THERE WAS A MATCH";
        } else {

            // No Match
            echo "NO MATCH";
        }

    } else {

        // Form Did Not Validate
        $this->load->view('signin/forgot');
    }
}

型号代码:

public function email_verify() {
    $sql = "SELECT * FROM admin, staff WHERE staff_email = ? OR admin_email = ?";
    $email = $this->input->post('email');
    $query = $this->db->query($sql, $email, $email);

    if($query->num_rows() == 1) {
        return true;
    } else {
        return false;
    }
}

错误:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

SELECT * FROM admin, staff WHERE staff_email = 'dsfsdf@test.com' OR admin_email = 

更新:我现在使用的固定查询是:

$sql = "SELECT admin_id, first_name FROM admin WHERE admin_email = ? UNION SELECT staff_id, first_name FROM staff WHERE staff_email = ?";
4

2 回答 2

1
 $query = $this->db->query($sql, array($email, $email));

试试看。应该修复 SQL 错误

于 2012-11-16T15:59:11.873 回答
1

$this->signin_model不是函数。它是一个对象。要调用模型函数,请将函数作为对象的属性调用:

$this->signin_model->email_verify()

编辑:第二个参数$this->db->query应该是一个数组。

$query = $this->db->query($sql, array($email, $email));

编辑 2:如果 2 个表之间没有链接,那么 aJOIN将无济于事。你可以试试a UNION

$sql = "SELECT * FROM staff WHERE staff_email = ? ".
    "UNION ALL SELECT * FROM admin WHERE admin_email = ?";
于 2012-11-16T15:45:57.053 回答