INNER JOIN查詢沒有結果

[英]No results from INNER JOIN query


Hi I have a basic search function with two fields for the user to enter a test name and result type. I have tried a number of methods like using dropdowns as filters, but this is the most simple way I have found. I'm using INNER Joins on 3 tables in the search query. At moment no errors regarding the sql syntax are coming up but no results are echoing too. I ran the query on the database and it returned the results fine. Am I missing something stupid, any help would be much appreciated.

嗨我有一個基本的搜索功能,有兩個字段供用戶輸入測試名稱和結果類型。我已經嘗試了許多方法,比如使用下拉菜單作為過濾器,但這是我找到的最簡單的方法。我在搜索查詢中的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>";

}
}

The tables are

表是

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

Here is the form Im using

這是我使用的表格

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 个解决方案

#1


0  

You test if $resultts and $tests exist, but then you use $resultt and $test in the sql statement itself. Additionally, you are testing on the value of $tests, not whether or not it's set. Try this instead:

您測試是否存在$ 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.
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/04/03/f8b9bb113ebb60c50f36ccbb205db962.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com