我想要一个搜索表单,用于搜索我的整个数据库。搜索表单应包含多个输入和选择标签。
自动取款机在提交所有可能的列和行组合之后向我显示。
这里是搜索字词部分:
<div style="width:1420px" class="wmfg_layout_2">
<form action="xxx.php" method="post">
<ul class="wmfg_questions">
<li class="wmfg_q">
<label class="wmfg_label">Suchkriterien auswählen</label>
<table class="wmfg_answers">
<tr class="wmfg_a">
<th>
<td><label class="wmfg_label_a" for="chk_search1"> search1 </label></td>
<td><input type="text" name="search1" size="25"></td>
</th>
<th>
<td><label class="wmfg_label_a" for="chk_search2"> search2 </label></td>
<td><input type="text" name="search2" size="25"></td>
</th>
<th>
<td><label class="wmfg_label_a" for="chk_search3"> Status </label></td>
<td>
<select name="search3" size="1">
<option disabled selected value> </option>;
<?php
foreach($result as $m)
{
echo "<option value=\"" . $m['resultID'] . "\">" .$m['result'] . "</option>";
}
?>
</td>
</select>
</th>
<th>
<td><label class="wmfg_label_a" for="chk_search4"> search4 </label></td>
<td>
<select name="search4" size="1">
<option disabled selected value> </option>;
<?php
foreach($result2 as $m)
{
echo "<option value=\"" . $m['result2ID'] . "\">" .$m['result2'] . "</option>";
}
?>
</th>
<th>
<td><label class="wmfg_label_a" for="chk_search5"> search </label></td>
<td>
<select name="search5" size="1">
<option disabled selected value> </option>;
<?php
foreach($result3 as $m)
{
echo "<option value=\"" . $m['resul3ID'] . "\">" .$m['result3'] . "</option>";
}
?>
</tr>
</th>
</table>
</td>
</select>
</th>
</li>
<li class='wmfg_qEnd'>
<input type='submit' name='suche' value='Anfrage senden'>
</li>
</ul>
</tr>
</form>
</div>
总是相同的结果,当我使用search和search1或仅search5等进行提交时,它会向我显示所有列和行的组合作为结果,而不是我搜索关键字时要显示的行 我认为我必须拆分搜索词并与条件相呼应,但不知道如何。 这是php的部分代码:
<?php
$search1=$_POST['search1'];
$search2=$_POST['search2'];
$search3=$_POST['search3'];
$search4=$_POST['search4'];
$search5=$_POST['search5'];
$search = $connection->query("SELECT col1, col2, col3, col4, col5, col5, col6, col7, col8 FROM table1, table2, table3
WHERE table1.col1 LIKE '%".$search1."%'
OR table1.col2 LIKE '%".$search2."%'
OR table1.col3 LIKE '".$search3."'
OR table1.col4 LIKE '".$search4."'
OR table1.col5 LIKE '".$search5."'");
$search->execute();
$result = $search->fetchAll(PDO::FETCH_ASSOC);
if (isset($_POST['search1'])
or isset($_POST['search2'])
or isset($_POST['search3'])
or isset($_POST['search4'])
or isset($_POST['search5'])){
foreach ($result as $row2) {
echo "<tr>";
echo "<td>".$row2['col1']."</td>";
echo "<td>".$row2['col2']."</td>";
echo "<td>".$row2['col3']."</td>";
echo "<td>".$row2['col4']."</td>";
echo "<td>".$row2['col5']."</td>";
echo "<td>".$row2['col6']."</td>";
echo "<td>".$row2['col7']."</td>";
echo "<td>".$row2['col8']."</td>";
echo "</tr>";
}
}
$connection = null;
?>
PHP代码的第二部分,它调用查询。
这是table1的示例表,仅出于理解目的而减少了列数,它具有更多的列和行。
表1
+-------+------+-----------+-----------+----------+
| PK_ID | name | FK_ID_TB2 | FK_ID_TB3 | status |
+-------+------+-----------+-----------+----------+
| 1 | TC | 1 | 1 | on hold |
| 2 | HTS | 2 | 2 | finished |
| 3 | HTOL | 3 | 3 | on going |
+-------+------+-----------+-----------+----------+
这是表2
+-------+------------+
| PK2_ID| technology |
+-------+------------+
| 1 | bla |
| 2 | bli |
| 3 | blubb |
+-------+------------+
和这里的table3
+-------+------------+
| PK3_ID| assignment |
+-------+------------+
| 1 | assign1 |
| 2 | assign2 |
| 3 | assign3 |
+-------+------------+
我有5种机制来搜索条目,它是一个过滤器-2种输入表单字段和3种选择。当我提交时,我想显示所有记录并输入我的标准。
例如,当我搜索“ TC”时,结果应为:
+-------+------+-----------+-----------+----------+
| PK_ID | name | technology | FK_ID_TB3 | status |
+-------+------+-----------+-----------+----------+
| 1 | TC | bla | assign | on hold |
+-------+------+-----------+-----------+----------+
当我搜索“ TC”和“进行中”时,应该显示row1和row3等。
这个问题可以理解吗?提前寻求帮助。祝你有美好的一天。
答案 0 :(得分:1)
我在这里更新我的答案您可以使用LOCATE instedof%并检查以下空值检查代码:
if(!isset($_POST['search1']))
$_POST['search1']='';
if(!isset($_POST['search2']))
$_POST['search2']='';
if($_POST['search1'] == '')
$search1='';
else
$search1=" LOCATE('".$_POST['search1']."',t1.name)>0 OR "
if($_POST['search2'] == '' )
$search2='';
else
$search2=" LOCATE('".$_POST['search2']."',t1.status)>0 OR "
$search3=$_POST['search3'];
$search4=$_POST['search4'];
$connection->query("
SELECT t1.PK_ID , t1.name , t2.technology, t3.assignment, t1.status FROM table1 t1, table2 t2, table3 t3
WHERE
t1.FK_ID_TB2=t2.PK2_ID
AND
t1.FK_ID_TB2=t3.PK3_ID
AND
(
".$search1."
".$search2."
t2.technology = '".$search3."'
OR t3.assignment = '".$search4."'
)");
以下示例取决于您的值http://sqlfiddle.com/#!9/027509/1