-1

嗨,我有一个基本的搜索功能,有两个字段供用户输入测试名称和结果类型。我尝试了很多方法,比如使用下拉菜单作为过滤器,但这是我找到的最简单的方法。我在搜索查询中的 3 个表上使用 INNER Joins。目前没有关于 sql 语法的错误出现,但也没有结果。我在数据库上运行了查询,它返回的结果很好。我是否错过了一些愚蠢的东西,任何帮助将不胜感激。

if (isset($_GET['action']) and $_GET['action'] =='search')
{

$resultts = mysql_real_escape_string(htmlentities(trim($_GET['result'])));
$tests = mysql_real_escape_string(htmlentities(trim($_GET['test'])));


if (isset($resultts) and ($tests))
{
$sql = "SELECT p.fileName, p.mimeType, p.dateCreated, t.testName, r.resultType
    FROM 'product_logs' AS p 
    INNER JOIN 'result' AS r 
    ON p.resultID=r.resultID
    INNER JOIN 'test' AS t
    ON r.testID=t.testID
    WHERE t.testName LIKE '%$tests%'
    AND r.resultType LIKE '%$resultts%'
    ORDER BY r.dateCreated DESC";

$result = mysqli_query($sql) or die ("error in query");


}

while ($row = mysqli_fetch_array($result))
{
echo "<tr>
<td>".$row['fileName']."</td>
<td>".$row['mimeType']."</td>
<td>".$row['dateCreated']."</td>
<td>".$row['testName']."</td>
<td>".$row['resultType']."</td>
</tr>";

}
}

这些表是

result

resultID    int(6)  NO  PRI NULL    auto_increment
resultType  varchar(15) NO      NULL    
resultDate  date    NO      NULL    
testID   int(11)    NO  MUL NULL    
testerID    int(11) NO  MUL NULL    
productID   int(11) NO  MUL NULL    

product_logs

logID       int(5)      NO  PRI NULL    auto_increment
mimeType    varchar(50) NO      NULL    
fileData    mediumblob  NO      NULL    
fileName    varchar(255)NO      NULL    
dateCreated date        NO      NULL    
resultID    int(11)    YES  MUL NULL

test

testID    int(5)        NO  PRI NULL    auto_increment
testName varchar(10)    NO      NULL    
testDate date           NO      NULL

这是我使用的表格

echo "<form action= results_page.php method= get>";
echo"<p>Search Product Logs:</p>";

echo"<div>";
echo"<label for=text>Please Enter Test Name:</label>";
echo"<input type=text name=test id=text/>";
echo"<label for=text>Please Enter Result Type:</lable>";
echo"<input type=text name=result id=text/>";
echo"<input type=hidden name=action value=search/>";
echo"<input type=submit value=Search>";
echo"</div>";
echo"</form>";
4

1 回答 1

0

您测试是否存在,然后$resultts在sql 语句本身中使用和。此外,您正在测试 的值,而不是它是否已设置。试试这个:$tests$resultt$test$tests

if (isset($resultts) and isset($tests))
{
    $sql = "SELECT p.fileName, p.mimeType, p.dateCreated, t.testName, r.resultType
        FROM 'product_logs' AS p 
        INNER JOIN 'result' AS r 
        ON p.resultID=r.resultID
        INNER JOIN 'test' AS t
        ON r.testID=t.testID
        WHERE t.testName LIKE '%$tests%'
        AND r.resultType LIKE '%$resultts%'
        ORDER BY r.dateCreated DESC";

    $result = mysqli_query($sql) or die ("error in query");
}else{
    // Insert error type message or handling here.
}
于 2013-04-03T18:34:42.723 回答