2011-12-06 62 views
0

我有3個搜索框,用戶可以將信息輸入到任意3個或全部3個搜索框中。使用like運算符的多搜索框

$user = $_POST['user']; 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 

$query = "SELECT user.user, user.firstname, user.lastname 
      FROM user WHERE type = 'Owner' AND user LIKE '%$user%'"; 

如果這個人是搜索所有3,我會如何把它們放入它?

$query = "SELECT user.user, user.firstname, user.lastname 
      FROM user WHERE type = 'Owner' AND user LIKE '%$user%' 
      OR/AND type = 'Owner' AND fname LIKE '%$firstname%' 
      OR/AND type = 'Owner' AND lname LIKE '%$lastname%'"; 

這是我有,但不能正常工作,但我需要這樣的東西或/與etce等..可有人請告訴我該怎麼辦呢?由於

+1

本文將結合WHERE語句** WARNING **你的代碼容易受到SQL注入攻擊。 –

+0

你爲什麼要檢查你的問題,並將標題和正文改爲asdaasdas asda sda sdas dasd asd asd? –

回答

1

如果值給出

$user  = mysql_real_escape_string($_POST['user']); 
$firstname = mysql_real_escape_string($_POST['firstname']); 
$lastname = mysql_real_escape_string($_POST['lastname']); 

$where[] = "type='Owner'"; 
if(strlen($user) > 0){ 
    $where[] = "user LIKE '%$user%'"; 
} 
if(strlen($firstname) > 0){ 
    $where[] = "firstname LIKE '%$firstname%'"; 
} 
if(strlen($lastname) > 0){ 
    $where[] = "lastname LIKE '%$lastname%'"; 
} 
$query = "SELECT user.user, user.firstname, user.lastname 
      FROM user WHERE ". implode(' AND ', $where);