0

ユーザーが入力する 3 つのテキスト ボックスを含む検索フォームがあります。

Keyword text box
Name text box
Date text box

現在、ユーザーの選択に対応するために、6 つの「if」を作成する必要があります。

そんなに多くの「if」を作る必要がない方法はありますか?

ユーザーが 3 つのテキスト ボックスすべてに入力することを選択した場合のコード例:

SELECT * FROM <table> WHERE keyword=<keyword txbox> AND Name = <name txtbox> AND Date = <date txtbox>
4

2 に答える 2

0

生の例、サニタイズなし:

<?php
$op=array('keyword','name','date');
$_POST=array('keyword'=>'a','name'=>'b','date'=>'c');

    $q="SELECT * FROM FOO WHERE ";

foreach ($op as $o){
    if(!empty($_POST[$o])){
    $q.= $o.' = "'.$_POST[$o].'" AND '; 
    }

}

$q=substr($q,0, -4);


echo $q; // SELECT * FROM FOO WHERE keyword = "a" AND name = "b" AND date = "c" 
?>
于 2013-03-19T02:49:56.900 に答える
0

この構成により、if ブロックが 3 つに減ります。私はphpを書いていないので、他の人が構文を教えてくれます。

select somefields
from sometables
where 1 = 3
and 
(
if they filled out the keyword
or keyword = that variable
if they filled out the name
or name = that variable
if they filled out the date
or date = that variable
)

必ずクエリ パラメータを使用してください。また、テキスト ボックスが完成しない場合の計画を立てておいてください。最後に、日付フィールドに時刻コンポーネントが含まれている場合は、クエリで処理します。

于 2013-03-19T03:02:55.247 に答える