我有2个下拉列表称为ddl1和ddl2,
当我选择ddl1时,它会填充该搜索的整个结果
当我选择ddl2来优化搜索时,不会进行任何更改。
如何从以下sql查询中实现精确搜索
<?php
$q=$_GET["q"];
$p=$_GET["p"];
require 'connection.php';
mysqli_select_db($con,"Faults" );
//where statement in the sql syntax will select where in db to get infor, use AND to add another condition
$sql="SELECT Products.Product_Name, Versions.Version, Platform.Platform_Name, Issues.Issue, Issues.Sub_Issue, Issues.Fix
FROM Solutions
INNER JOIN Products
ON Solutions.Product = Products.Product_id
INNER JOIN Versions
ON Solutions.Product_Version = Versions.Version_id
INNER JOIN Platform
ON Solutions.Product_Platform = Platform.Platform_id
INNER JOIN Issues
ON Solutions.Product_Issue = Issues.Issue_id
WHERE Product = '".$q."'
OR (Product_Issue = '".$p."'
OR Product_Issue ='".$p."')";
$result = mysqli_query($con,$sql);
//below is the echo statment to create the results in a table format, list collumn titles
echo "<table id=tables border='1'>
<tr>
<th>Products</th>
<th>Version</th>
<th>Platform</th>
<th>Issue</th>
<th>Sub Issue</th>
<th>Fix</th>
</tr>";
//below is script to list reults in a table format, $row [row name on table]
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['Product_Name'] . "</td>";
echo "<td>" . $row['Version'] . "</td>";
echo "<td>" . $row['Platform_Name'] . "</td>";
echo "<td>" . $row['Issue'] . "</td>";
echo "<td>" . $row['Sub_Issue'] . "</td>";
echo "<td><a href=\"idfix.php?Fix=" . nl2br($row['Fix']) . "\">Fix</a></td>";
echo "</tr>";
}
echo "</table>";
// below closes the coonection to mysql
?>
JS代码
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+ product.value +"&" +"p="+ issue.value ,true);
xmlhttp.send();
}
答案 0 :(得分:0)
我假设$ q是下拉列表1的值,$ p是下拉列表2的值。
实现所需内容的一种方法是在ddl2中未选择任何值时将$ p默认为LIKE
通配符,然后在选择某些内容时输入ddl2值。
您查询的最后一部分可能就是:
WHERE Product = '".$q."'
AND Product_Issue LIKE '".$p."'
因此,如果未选择ddl2值,则会得到LIKE '%'
,基本上不会过滤,如果选择了某些内容,则会得到LIKE 'value'
,这通常等同于{{ 1}}你目前正在寻找(有一些差异,但通常都不会影响像这样的一般查询)。使用= 'value'
还应该允许您设置的任何表索引继续工作。
要查看LIKE
与LIKE
的任何影响,请参阅http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
您还可以根据ddl2值的选择,动态地添加查询的ddl2部分(附加到查询字符串)。