简体   繁体   中英

php sql query not returning correct results

I have a page with 3 dropdowns. Team1, Team2 & Venue

在此处输入图片说明

When the user clicks view, I then query the DB returning results for team1 against team2 either playing at home or away or both (depending on users selection)

The code Im using to execute the query follows

if($venue = "hometeam"){
        $result= " SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2'" or die(mysql_error());
    }

    else if($venue = "awayteam"){
        $result = " SELECT *
FROM `results`
WHERE `awayteam` = '$team1' && `hometeam` = '$team2'"or die(mysql_error());
    }

    else if($venue ="all"){
        $result = " SELECT *
FROM `results`
WHERE (`hometeam` = '$team1' AND `awayteam` = '$team1') OR (`hometeam` = '$team2' AND `awayteam` = '$team2')"or die(mysql_error());

    }

The Problem

Regardless if the user selects venue away or both home & away the result returned is always team1 as the hometeam as you can see in the image below:

在此处输入图片说明

In the above example I selected Team1 as Stormers and Team2 as Sharks, I selected venue away to display Stormers record against Sharks when they are playing away from home, but as you can see from the image Stormers still gets displayed as the hometeam.

If anyone can tell me what I am doing wrong or point me in the right direction it would be greatly appreciated.

Thank you in advance

You are doing an assignment operation on your if statements. Make use of == instead of =

The

if($venue = "hometeam"){

should be

if($venue == "hometeam"){
         //^------ Add one more like this. Do this for your `elseif` too 

You're currently "assigning" instead of "comparing" with your conditional statements. It being in the "plural" form. You have three which should be == instead of =

= is an assignment operator , while == is a comparison operator .

Missing an extra = see arrows ^

if($venue = "hometeam")
           ^

else if($venue = "awayteam")
                ^

else if($venue = "all")
                ^

Rewrite:

if($venue == "hometeam"){
        $result= " SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2'" or die(mysql_error());
    }

    else if($venue == "awayteam"){
        $result = " SELECT *
FROM `results`
WHERE `awayteam` = '$team1' && `hometeam` = '$team2'"or die(mysql_error());
    }

    else if($venue == "all"){
        $result = " SELECT *
FROM `results`
WHERE (`hometeam` = '$team1' AND `awayteam` = '$team1') OR (`hometeam` = '$team2' AND `awayteam` = '$team2')"or die(mysql_error());

    }

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