PHP附加if语句阻塞第一个

时间:2014-02-14 10:06:54

标签: php sql

我有以下声明:

if (isset($_POST['name'])) { 
    $name = $_POST['name']; 
    $sql = "SELECT * FROM patients WHERE name LIKE '{$name}' "; 
} elseif (isset($_POST['surname'])) { 
    $surname = $_POST['surname']; 
    $sql = "SELECT * FROM patients WHERE surname LIKE '{$surname}' "; 
} elseif (isset($_POST['pesel'])) { 
    $pesel = $_POST['pesel']; 
    $sql = "SELECT * FROM patients WHERE pesel LIKE '{$pesel}' "; 
}

我有3个搜索表单。但只有第一个起作用(名字)。其他不回应。如何改变?

5 个答案:

答案 0 :(得分:2)

请检查是否所有$ _POST []变量未同时设置。因为,$ _POST ['name']是第一个变量,如果设置了,则不会检查其他条件。

答案 1 :(得分:2)

浏览器将在请求中发送输入字段,即使它为空

您无法使用isset测试字符串是否为空。您也需要!== ""

答案 2 :(得分:1)

try this....  
if(isset($_POST['name'])) {  
$name = $_POST['name'];  
echo $name;  
$sql = "SELECT * FROM patients WHERE name LIKE '{$name}' ";  
}  
else if(isset($_POST['surname'])) {   
$surname = $_POST['surname'];   
echo $surname;  
$sql = "SELECT * FROM patients WHERE surname LIKE '{$surname}' ";  
}  
else if(isset($_POST['pesel'])) {  
$pesel = $_POST['pesel'];
echo $pesel;  
$sql = "SELECT * FROM patients WHERE pesel LIKE '{$pesel}' ";  
}   

答案 3 :(得分:0)

您可以在单个查询中使用多个like条件

 $sql = "SELECT * FROM patients WHERE name LIKE '{$name}' OR LIKE '{$surname}' OR LIKE '{$pesel}' ";

答案 4 :(得分:-2)

试试这个:

if(isset($_POST['name'])||isset($_POST['surname'])||isset($_POST['pesel'])) { 
    $name = $_POST['name']; 
    $sql = "SELECT * FROM patients WHERE name LIKE '{$name}' or surname LIKE '{$surname}' or pesel LIKE '{$pesel}'"; 
}