我正在做一個網站,用戶可以選擇一個出租物業,有兩個領域,他們可以選擇,傢俱和寵物。在兩個選擇框中的選項是是和否。我的SQL語句有什麼問題?
<select name="furnished">
<option value="">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<select name="pets">
<option value="">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
我正在編寫基於用戶在這些字段中選擇的SQL語句。
$sql = 'SELECT *
FROM properties
WHERE num_bedrooms >= ' . $_GET['num_bedrooms'] .
' AND num_bathrooms >= ' . $_GET['num_bathrooms'];
if($_GET['furnished'] == 'yes') { //if the furnished is set to yes
$sql .= ' AND furnished = "yes" OR furnished = "partially"';
} else if($_GET['furnished'] == 'no') { //if the furnished is set to no
$sql .= ' AND furnished = "no" OR furnished = "description"';
}
if($_GET['pets'] == 'yes') { //if the pets is set to yes
$sql .= ' AND pets = "yes" OR pets = "cats" OR pets = "dogs"';
} else if($_GET['pets'] == 'no') { //if the pets is set to no
$sql .= ' AND pets = "no" OR pets = "description"';
}
如果用戶是選擇有傢俱,我希望它顯示配(是),或者是部分傢俱(部分)的所有屬性。如果用戶選擇否,我希望它顯示所有未提供的屬性(否)或具有特殊描述(描述)的屬性。
如果用戶爲寵物選擇是,我希望它顯示允許所有寵物(是)或只允許貓(貓)或只允許狗(狗)的所有屬性。如果用戶選擇否,我希望它顯示所有不允許寵物的屬性(否)或具有特殊描述(描述)的屬性。
作爲一個例子,這是輸出的SQL語句,如果用戶選擇上提供的是的是的寵物:
SELECT * FROM properties
WHERE num_bedrooms >= 1
AND num_bathrooms >= 1
AND furnished = "yes" OR furnished = "partially"
AND pets = "yes" OR pets = "cats" OR pets = "dogs"
的問題是,它現在將返回滿足裝飾或寵物的任何結果在where條款中的要求,而我需要它返回符合where條款中的傢俱和寵物要求的結果。
所以它現在會返回一個結果,說有Furnished設置爲yes,寵物設置爲no。 我怎樣才能返回所有結果與傢俱設置爲是和寵物設置爲是?
我的SQL語句有什麼問題?
你不會像你應該逃避輸入變量。看看[mysql_real_escape_string()](http://php.net/manual/en/function.mysql-real-escape-string.php)或[PDO :: quote()](http:// php。 net/manual/en/pdo.quote.php)或數據庫庫中使用的轉義/引用方法... – rodneyrehm