2013-03-18 35 views
0

我得到這個錯誤:PHP如何編碼(轉義)@,用於電子郵件驗證?

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 '@gmail.com' at line 1

下面的代碼:

$email = $this->input->post('email'); 

       $checkEmail = $this->crud_model->retrieve_where('employee', 'email', 'email', $email); 

crud_model:

function retrieve_where($table, $table_id, $table_name, $value) { 
     $table = $this->db->query('Select * FROM ' . $table . ' Where ' . $table_id . ' = ' . $value); 
     $records = array(); 
     foreach ($table->result() as $row) { 
      $records[] = $row->$table_name; 
     } 
     return $records; 
    } 
+0

基本上用''來包含你的值選擇* FROM employee where email ='[email protected]' – ITroubs 2013-03-18 13:19:07

+0

引用字符串:'select * FROM employee Where email ='b @ gmail.com''你不顯示你的retieve_where()做了什麼,但是我堅持它不使用預準備語句,或者正確引用字符串 – 2013-03-18 13:20:10

+0

引號去哪裏? $ this-> crud_model-> retrieve_where('employee','email','email',$ email); – Waygood 2013-03-18 13:21:04

回答

2

您嘗試執行的查詢是:

SELECT * FROM Email WHERE Email = [email protected]

電子郵件在這種情況下是一個字符串,並且需要引用一個字符串。

function retrieve_where($table, $table_id, $table_name, $value) { 
    $table = $this->db->query("Select * FROM " . mysql_real_escape_string($table) . " Where " . mysql_real_escape_string($table_id) . " = '" . mysql_real_escape_string($value) ."'"); 
    $records = array(); 
    foreach ($table->result() as $row) { 
     $records[] = $row->$table_name; 
    } 
    return $records; 
} 

不要忘了跳過您的查詢mysql_real_escape_string()。它會保護你免受注射。

+1

忘了逃脫值! http://xkcd.com/327/ – Waygood 2013-03-18 13:27:34

+0

也..如果這是一個通用函數,它不會工作,如果該值是一個數字。 – lemil77 2013-03-18 13:28:56

+0

我相信它會奏效。它在我測試它時做到了。 – Waygood 2013-03-18 13:30:16