简体   繁体   中英

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.

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. Additionally, you are testing on the value of $tests , not whether or not it's set. 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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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