我有一个表单通过两个输入字段传递两个值,数据来自数据库表中预先加载的16个字段。此表单的目的是将db字段和数据传递给php脚本,该脚本将根据这些输入字段和值进行sql查询,并使用给定的过滤器打印过滤的表条目
我的数据库表结构如下:
field1,field2,field3,field4,field5,field6,field7,field8, field9,field10,field11,field12,field13,field14,field15,field16
我目前正在使用这个:
<?php
$searchfield1='';
$searchfield2='';
$searchquery1='';
$searchquery2='';
//setting value of variable $searchquery1 depending on the incoming value
if ($_POST=['field1Name1']) {$searchfield1='field1'; $searchquery1='field1Data';}
else if ($_POST=['field2Name1']) {$searchfield1='field2'; $searchquery1='field2Data';}
else if ($_POST=['field3Name1']) {$searchfield1='field3'; $searchquery1='field3Data';}
else if ($_POST=['field4Name1']) {$searchfield1='field4'; $searchquery1='field4Data';}
else if ($_POST=['field5Name1']) {$searchfield1='field5'; $searchquery1='field5Data';}
else if ($_POST=['field6Name1']) {$searchfield1='field6'; $searchquery1='field6Data';}
else if ($_POST=['field7Name1']) {$searchfield1='field7'; $searchquery1='field7Data';}
else if ($_POST=['field8Name1']) {$searchfield1='field8'; $searchquery1='field8Data';}
else if ($_POST=['field9Name1']) {$searchfield1='field9'; $searchquery1='field9Data';}
else if ($_POST=['field10Name1']) {$searchfield1='field10'; $searchquery1='field10Data';}
else if ($_POST=['field11Name1']) {$searchfield1='field11'; $searchquery1='field11Data';}
else if ($_POST=['field12Name1']) {$searchfield1='field12'; $searchquery1='field12Data';}
else if ($_POST=['field13Name1']) {$searchfield1='field13'; $searchquery1='field13Data';}
else if ($_POST=['field14Name1']) {$searchfield1='field14'; $searchquery1='field14Data';}
else if ($_POST=['field15Name1']) {$searchfield1='field15'; $searchquery1='field15Data';}
else if ($_POST=['field16Name1']) {$searchfield1='field16'; $searchquery1='field16Data';}
//setting value of variable $searchquery2 depending on the incoming value
if ($_POST=['field1Name2']) {$searchfield2='field1'; $searchquery2='field1Data';}
else if ($_POST=['field2Name2']) {$serachfield2='field2'; $searchquery1='field2Data';}
else if ($_POST=['field3Name2']) {$serachfield2='field3'; $searchquery1='field3Data';}
else if ($_POST=['field4Name2']) {$serachfield2='field4'; $searchquery1='field4Data';}
else if ($_POST=['field5Name2']) {$serachfield2='field5'; $searchquery1='field5Data';}
else if ($_POST=['field6Name2']) {$serachfield2='field6'; $searchquery1='field6Data';}
else if ($_POST=['field7Name2']) {$serachfield2='field7'; $searchquery1='field7Data';}
else if ($_POST=['field8Name2']) {$serachfield2='field8'; $searchquery1='field8Data';}
else if ($_POST=['field9Name2']) {$serachfield2='field9'; $searchquery1='field9Data';}
else if ($_POST=['field10Name2']) {$serachfield2='field10'; $searchquery1='field10Data';}
else if ($_POST=['field11Name2']) {$serachfield2='field11'; $searchquery1='field11Data';}
else if ($_POST=['field12Name2']) {$serachfield2='field12'; $searchquery1='field12Data';}
else if ($_POST=['field13Name2']) {$serachfield2='field13'; $searchquery1='field13Data';}
else if ($_POST=['field14Name2']) {$serachfield2='field14'; $searchquery1='field14Data';}
else if ($_POST=['field15Name2']) {$serachfield2='field15'; $searchquery1='field15Data';}
else if ($_POST=['field16Name2']) {$serachfield2='field16'; $searchquery1='field16Data';}
include 'dbase.php';
$data = "SELECT * FROM SOMETABLE WHERE $searchfield1='$searchquery1' AND $serachfield2='$searchquery2' ORDER BY ID DESC";
$result = $conn->query($data);
if ($result->num_rows > 0) {
// display data in table
echo 'table HTML code goes here'; ?>
虽然,我能够通过上面的代码获得所需的结果,但我知道必须有更好的方法来实现这一点,我对堆栈社区的问题是,那有更好的方法吗?所以我可以缩短这个代码或者使它更有效和更精确。实际上我想在这个脚本中使用更多的过滤器来对数据进行排序,但代码将变得更长更长,因为我将使用上述格式应用过滤器,我已经用于定义变量。
让我澄清一下,该字段没有编号,如上面的代码所示,它们被命名为一些文本字段,例如客户,地址,产品,货仓,销售,发货等。
答案 0 :(得分:0)
使用for循环:
for ($i=1; $i<=16; $i++) {
if ($_POST=['field'.$i.'Name1']) {
$searchfield1='field'.$i;
$searchquery1='field'.$i.'Data';
}
if ($_POST=['field'.$i.'Name2']) {
$searchfield2='field'.$i;
$searchquery2='field'.$i.'Data';
}
}