INNER JOIN查询没有结果

时间:2013-04-03 18:20:13

标签: php mysql inner-join

您好我有一个基本搜索功能,有两个字段供用户输入测试名称和结果类型。我尝试了许多方法,比如使用下拉菜单作为过滤器,但这是我找到的最简单的方法。我在搜索查询中的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>";

1 个答案:

答案 0 :(得分:0)

您测试$resultts$tests是否存在,但是您在sql语句本身中使用$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.
}