sql中對於傳過來的空值判斷
我有四個這樣的輸入值
action到處理的php頁面
有沒有什麼方法能夠合理的讓php頁面的sql語句判斷當那個欄位為空白時,就不以那個為搜尋的條件了
除了窮舉,還有什麼方法?
$sql="select * from table where ..."
執行個體:
html
chuli.php
//串連資料庫部分省略
$sql="select * from student where name='$name' and age='$age' and sex='$sex' and height='$height';// 這個是所有的
$result = mysql_query($sql,$con);
//除了窮舉以外,有沒有什麼方法,判斷那幾個Input資料為空白的時候的$sql進行判斷的方法?
//如果是4個input的情況下的時候是 4+6+4+1+1=16種情況,應該對吧? c41+c42+c43+c44+1=4+6+4+1+1=16?
------解決方案--------------------
PHP code
foreach(array_diff($_POST, array('')) as $k=>$v) $r[] = "$k='$v'";$sql = "select * from student where ". join(' and ', $r);
------解決方案--------------------
應該是,上面少給一個''
PHP code
$sql="select * from student where 1=1";if(!empty($_POST['name'])) $sql=" and name='$_POST[name]'";if(!empty($_POST['age'])) $sql=" and age='$_POST[age]'";if(!empty($_POST['sex'])) $sql=" and sex='$_POST[sex]'";if(!empty($_POST['height'])) $sql=" and height='$_POST[height]'";