简体   繁体   English

没有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. 我在搜索查询的3个表上使用INNER Joins。 At moment no errors regarding the sql syntax are coming up but no results are echoing too. 目前,关于sql语法的错误没有出现,但是也没有回显任何结果。 I ran the query on the database and it returned the results fine. 我在数据库上运行查询,它返回的结果很好。 Am I missing something stupid, any help would be much appreciated. 我是否缺少一些愚蠢的东西,我们将不胜感激。

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>";

You test if $resultts and $tests exist, but then you use $resultt and $test in the sql statement itself. 您测试$resultts$tests存在,但随后在sql语句本身中使用$resultt$test Additionally, you are testing on the value of $tests , not whether or not it's set. 此外,您正在测试$tests的值,而不是是否设置了该值。 Try this instead: 尝试以下方法:

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM