简体   繁体   中英

My SQL query will not output the field result correctly

I am trying to run a query which will provide a user on the website with:

  • The competition name
  • The image title
  • The result (1st place, 2nd place or 3rd)

So far I have managed to pull out the below result:

网页截图

<div class="grid-2">
    <h3>Competition Entries</h3>
        <form action = "" method = "POST">
        <select name="competitionID">
        <option value="">Select Competition</option>
        <option value="1">Winter Warmer</option>
        <option value="2">Fresh New Year</option>
        <option value="3">Month of Love</option>
        <option value="4">Seaside Scenery</option>
        </select>
        </fieldset>
     </form>
<?php

    $query = "SELECT `fldCompName`, `fldName`, `fldResult` FROM `tblMembEntComp` JOIN `tblCompetition` ON `tblMembEntComp`.`fldCompID`=`tblCompetition`.`fldCompID` JOIN `tblImage` ON `tblMembEntComp`.`fldMemberID`=`tblImage`.`fldMemberID` ORDER BY `fldResult` DESC LIMIT 3";

$result = $conn -> query($query);

while($row = $result -> fetch_assoc()) 
    {
        echo $row['fldCompName']." ".$row['fldName']." ".$row['fldResult']."<br>";
    } 
    ?>
    <button>View Competition Winners</button>
</div>

However, the results are all showing up as 17 for all of those top three entries when in the tbmMembEntComp fldResult I have 19, 17 and 11. Can someone just highlight where I've gone wrong and give guidance on what the query should be. phpMyAdmin中的表

You are not limiting the results to one specific competition, therefore it gives all the joined rows between member table and competition table.

Something like this (example only)

SELECT `fldCompName`, `fldName`, `fldResult`  
FROM `tblMembEntComp`  
JOIN `tblCompetition` ON `tblMembEntComp`.`fldCompID`=`tblCompetition`.`fldCompID`
-- EXAMPLE
AND `tblCompetition`.`fldCompID` = {someX}
------ 
JOIN `tblImage` ON tblMembEntComp`.`fldMemberID`=`tblImage`.`fldMemberID` 
ORDER BY `fldResult` DESC LIMIT 3";

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