2017-04-03 10 views
0

私はユーザーが情報を入力するフォームを用意しています。フォームが送信された後、ユーザーが入力した値でデータベースに照会する必要があります。MYSQLクエリでヌル値を無視します。

私の問題は、ユーザーが入力した値の一部がnullの場合は、クエリから削除する必要があります。

これは私のコードです:$negocio$imovel$concelho$freguesianullに等しい場合

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"; 
} 

を想像し、クエリは次のようになります。

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

私はこれをどのように行うことができますか?

+0

あなたはに沿って、OR'の代わりにAND'の 'の'使用するか、CASEとサブクエリを使用することができます'IS NULL'を返します。変数が文字列であるかどうかは不明であり、dbスキーマはこのためです。 –

+0

申し訳ありませんが、私の変数はintです。 –

+0

Alexandre 2つの答えがあり、どちらも正しいです。混乱しているともう一度それらを読んでください。 –

答えて

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

各句に入力NULLチェックを追加できます。だから、あなたがこれを行う例:

(distrito = $distrito or $distrito IS NULL) 

または多分:

データタイプによっては、実際の入力をするために使用されている
(distrito = $distrito or $distrito = '') 

distrito = $distrito 

あなたは代わりにこれを行う可能性がありますこのようなクエリを手動で作成するときには、いくつかの調整やデバッグが必要になるかもしれません(クエリパラメータを持つプリペアドステートメントを使用することで、よりクリーンで安全です)アイデアはいずれにしても同じです。

基本的には、値に基づいて行を一致させるか、値の不足に基づいて行を一致させるように指示しています。したがって、指定された句に対して、指定された値がnull /空の場合、すべての行が一致し、句が偽になります。

+0

私はあなたの考えを理解していません。 NULL値で照会しようとすると、NULL値を持つ結果が返されます。 –

+0

@AlexandreCristo:考えられるのは、NULL値を指定して* all *行が返された場合、その検索値はnot検索に使用されています。指定した値( 'NULL'など)に対して'(distrito = $ distritoまたは$ distrito IS NULL)が何であるかを考えてみましょう。 – David

関連する問題