Php PDO查询取决于完成了哪些输入

Php PDO查询取决于完成了哪些输入,php,mysql,pdo,mysqli,Php,Mysql,Pdo,Mysqli,您好,我有一个包含6个字段的表单: Age1 Age2 Client type Destination Score1 Score2 我想根据完成的字段创建一个查询,但我不知道如何仅通过为每个字段创建一个查询来创建一个查询。这些表单字段是我的2个表数据库中的列 $age1=$_POST['age1']; $age2=$_POST['age2']; $score1=$_POST['score1']; $score2=$_POST['score2'];

您好,我有一个包含6个字段的表单:

    Age1
    Age2
    Client type
    Destination
    Score1
    Score2
我想根据完成的字段创建一个查询,但我不知道如何仅通过为每个字段创建一个查询来创建一个查询。这些表单字段是我的2个表数据库中的列

$age1=$_POST['age1'];
$age2=$_POST['age2'];
$score1=$_POST['score1'];
$score2=$_POST['score2'];
$destination=$_POST['destionation'];
$client_type=$_POST['client_type'];

if ($age1!='' and $age2!='')
{
    $age_sql=" where TIMESTAMPDIFF(year, Bday,CURDATE())>=$age1 and TIMESTAMPDIFF(year, Bday,CURDATE())<=$age2"; 
}
if ($destination!='')
{
$dest_sql='Inner Join Leg_Client_Destinatie 
                where  Leg_Client_Destinatie.Destinatie="'.$destinatie.'"
                and Leg_Client_Destinatie.ID=Persoane.ID';  
}
$stmt = $dbh->prepare("SELECT * from Persoane $dest_sql $varsta_sql");    
                    $stmt->execute(); 
                while ($row = $stmt->fetch())
                {
                }
$age1=$\u POST['age1'];
$age2=$_POST['age2'];
$score1=$_POST['score1'];
$score2=$_POST['score2'];
$destination=$_POST['destination'];
$client\u type=$\u POST['client\u type'];
如果($age1!=''和$age2!='')
{

$age\u sql=“where TIMESTAMPDIFF(year,Bday,CURDATE())>=$age1和TIMESTAMPDIFF(year,Bday,CURDATE())我成功地使它工作起来。我打赌有更优雅的方法来实现它,例如使用数组,但atm就是这样。如果有人有其他想法,请随意发布。Thnx

$age1=$_POST['age1'];
$age2=$_POST['age2'];
$score1=$_POST['score1'];
$score2=$_POST['score2'];
$destination=$_POST['destionation'];
$client_type=$_POST['client_type'];

$sql=("SELECT * From Persoane $join WHERE Persoane.ID IS NOT NULL $join2");
if ($destination!='')
{
$join='Inner Join Leg_Client_Destination 
                ON  Leg_Client_Destination.Destination="'.$destination.'"
                ';  
$join2='and Leg_Client_Destination.ID=Persoane.ID';
} 
if ($age1!='' and $age2!='')
{
    $sql .= " AND TIMESTAMPDIFF(year, Bday,CURDATE())>=$age1 and TIMESTAMPDIFF(year, Bday,CURDATE())<=$age2"; 
}   
if ($score1!='' and $score2!='')
{
    $sql .= " AND score_T>=$score1 and score_T<=$score2"; 
}
if ($client_type!='')
{
    $sql .= " AND Client_Type=$client_type"; 
}       
                    $stmt=$dbh->prepare($sql);
                    $stmt->execute(); 
                while ($row = $stmt->fetch())
                {
$age1=$\u POST['age1'];
$age2=$_POST['age2'];
$score1=$_POST['score1'];
$score2=$_POST['score2'];
$destination=$_POST['destination'];
$client\u type=$\u POST['client\u type'];
$sql=(“选择*来自Persoane$join,其中Persoane.ID不是NULL$join2”);
如果($destination!='')
{
$join='内部连接分支\u客户端\u目的地
在支腿上的客户机目的地。目的地=“”.$Destination.”
';  
$join2='and Leg_Client_Destination.ID=Persoane.ID';
} 
如果($age1!=''和$age2!='')
{

$sql.=”和TIMESTAMPDIFF(year,Bday,CURDATE())>=$age1和TIMESTAMPDIFF(year,Bday,CURDATE())=$score1和score\t这就是您所能做的:根据用户选择的内容动态构建查询字符串。否则,您将无法编写包含处理所有可能选项组合的逻辑的异常丑陋/庞大的查询。好的,但请检查我的示例,假设我在需要的sql年龄完成了目标和年龄
而不是
where
,因为我在
$dest_sql
上使用了
where
。是的。如果动态构建查询,则必须确保构建语法有效的查询。您使用来自外部世界的代码构建sql语句。这会让您面临sql注入攻击。请阅读fin我知道如何安全地进行SQL查询。我将绑定值。这是np。我的问题是如何实际构建查询:DWell由于您没有准备参数,所以您也打开了SQL注入的大门。我知道我在绑定参数之前已跳过了代码,并且此代码仍处于登录状态:p
$stmt->bindParam(':age1',$age1);