2017-04-03 94 views
0

我有一個表單供用戶輸入一些信息。表單提交後,它應該用用戶輸入的值查詢數據庫。忽略MYSQL查詢中的空值

這裏我的問題是,如果用戶輸入的某些值爲空,它應該從查詢中刪除。

這是我的代碼:

if(isset($_POST['submit'])) 
{ 
include("../includes/header.php"); 
include ("../scripts/db/connect.php"); 

//Gets variables from $_POST 
$negocio = $_POST['negocio']; 
$imovel = $_POST['imovel']; 
$distrito = $_POST['distrito']; 
$concelho = $_POST['concelho']; 
$freguesia = $_POST['freguesia']; 

$query = "SELECT * FROM imoveis WHERE negocio = $negocio and imovel = $imovel and distrito = $distrito and concelho = $concelho and freguesia = $freguesia"; 
} 

試想一下,如果$negocio$imovel$concelho$freguesia等於null,查詢應該是:

$query = "SELECT * FROM imoveis WHERE distrito = $distrito; 

我怎樣才能做到這一點?

+0

你可以使用'或'的,而不是'和'的或使用子查詢用的情況下,沿用'IS NULL'。你的變量不清楚,如果他們是字符串或不是這個數據庫模式。 –

+0

對不起,我的變量只是int。 –

+0

Alexandre你有兩個答案,兩者都是正確的,如果你感到困惑,再次閱讀它們 –

回答

3

生成您的查詢字符串dynamcilly取決於其值設置 或不爲空,而不是使用該查詢

運行這段代碼在一個單獨的文件,你會明白點,刪除或之後添加評論任何變量,($名稱,$街道,$地址或$資質)

// you will see query will change depending on the set variable, 
//I am using these name you can use any name for your variable 

$name='my name'; 
//$address='some where on earth'; 
$street='this is my street'; 
//$qualification='i am very much qualified'; 

//now create the array only with the values which are not empty or not nul, 
//I am using empty you can use null if you want with this example you can use any thing. 
if(!empty($name)) $query_string_second_part[]=" AND name = '$name'"; 
if(!empty($address)) $query_string_second_part[]=" AND address = '$address'"; 
if(!empty($street)) $query_string_second_part[]=" AND street = '$street'"; 
if(!empty($qualification)) $query_string_second_part[]=" AND qualification = '$qualification'"; 

//hand type the first part for the query 
$query_string_First_Part= "SELECT * FROM myTableName WHERE"; 
//Implode the array, if you want to see how it look like use echo, 
$query_string_second_part= implode(" ", $query_string_second_part); 
//as you can see we are adding AND with every value, so we need to remove the first AND 
//with one space 
//Make sure you give space in the second parameter. else it wont work means "" not correct but " " is correct 
//Hint --> use one space in between the double qoutes 
$query_string_second_part= preg_replace("/AND/", " ", $query_string_second_part, 1); 

//Join the first and second part together to create a full query 
$query_string=$query_string_First_Part.$query_string_second_part; 
echo ($query_string);//see how our query look like at the moment 
0

您可以將輸入空檢查添加到每個子句。因此,例如當你這樣做:

distrito = $distrito 

你可能反而這樣做:

(distrito = $distrito or $distrito IS NULL) 

或者是:

(distrito = $distrito or $distrito = '') 

根據數據類型,實際的輸入被用來構建查詢等等。當手動構建一個像這樣的查詢時,可能需要進行一些調整和調試(我懷疑使用帶有查詢參數的準備語句會使這個更乾淨,更安全),但是想法也是一樣的。

基本上,您正在指示它匹配基於該值的行,或者匹配基於缺乏值的行。因此,對於任何給定的子句,如果提供的值爲空/空,則所有行匹配並且子句變得沒有意義。

+0

我不明白你的想法。如果我嘗試用空值進行查詢,它將返回空值爲 –

+0

@AlexandreCristo:這個想法是,如果你提供了一個NULL值,那麼將返回所有*行,因爲該搜索值被解釋爲不是已被用於搜索。考慮給你提供的任何給定值'NULL'或其他值,'(distrito = $ distrito或$ distrito IS NULL)'。 – David