表单和后续sql语句中的多个未定义变量


Multi undefined variables from a form and subsequent sql statement

我寻求的结果是循环遍历从isset或true表单中获取的项,然后让sql语句查询数据库。复杂之处在于,我有9个字段,用户可以选择一个或多个或所有字段(这是一个搜索数据库,但每次选择额外的字段时,它都会细化搜索)。返回true的代码部分我可以通过使用foreach循环来了解情况,但如果结果不同,如何将其链接到SQL查询?

根据输入的字段生成不同的查询。幸运的是,这在SQL中并不太难:所有这些字段都在WHERE子句中:

$where = [ 'foo' => 'bar', 'baz' => 0 ];
$sth = $db->prepare( "SELECT * FROM $table WHERE " .
    implode( " AND ",
        array_map( function($i) { return "$i=?"; }, array_keys( $where ) ) )
    );
$sth->execute( array_values( $where ) );

当然,如果字段之间存在关系,查询可能会变得更加复杂,但这就是它的要点

学习这需要时间和耐心我已经跳过了表格中的变量

    if(isset($_POST['Search'])){
    $packID = $_POST['packID'];
    $supplier_name = $_POST['supplier_name'];
    $timber_species = $_POST['timber_species'];
    $timber_product = $_POST['timber_product'];
    $timber_grade = $_POST['timber_grade'];
    $timber_finish = $_POST['timber_finish'];
    $timber_treatment = $_POST['timber_treatment'];
    $width = $_POST['width'];
    $thickness = $_POST['thickness'];
    $length = $_POST['length'];
    $markup = $_POST['markup'];
} else{
    $packID="";
    $supplier_name="";
    $timber_species="";
    $timber_product="";
    $timber_grade="";
    $timber_finish="";
    $timber_treatment="";
    $width= "";
    $thickness= "";
    $length="";
}

当变量可能被设置,也可能没有被设置时,你会如何写这个肯尼。我必须承认我是个新手,而且很想学习。这需要时间和耐心。