Marilee
Marilee

Reputation: 1598

PHP query giving wrong results

This is a follow up question on a previous question I asked SQL query returning wrong results

Quick Background of Question

On my site a user can select players out of a squad of 32 players. There are 15 positions in the starting lineup so out of the 32 player squad 15 players need to be selected. 1 player can play more than one position

When I run the following PHP query

$sql1 = mysql_query("SELECT * FROM `allsquads` WHERE `Team` = '$t1select' 
                    AND `Position` = '$position[$i]' 
                    OR `Secondary` = '$position[$i]'") or die(mysql_error());

The Problem

The query works but it does not return players from only $t1select which is the team the user selected, it displays players from ALL teams in my database.

Any idea how I can fix this or why this is happening.

Thank you in advance

Upvotes: 0

Views: 128

Answers (2)

GautamD31
GautamD31

Reputation: 28763

May be you should write your query like

$sql1 = mysql_query("SELECT * FROM `allsquads` WHERE `Team` = '".$t1select."' 
                AND ( `Position` = '$position[$i]' 
                OR `Secondary` = '$position[$i]' ) ") or die(mysql_error());

Because your OR condition will be between Position and Secondary.As you have written it will take

`Team` = '".$t1select."' AND `Position` = '$position[$i]'

And then apply Or condition to that result.

Note :mysql functions are deprecated as of PHP 5.5 so you need to use either of mysqli or PDO statements.

Upvotes: 2

Strawberry
Strawberry

Reputation: 33935

... WHERE Team = '$t1select'
            AND '$position[$i]' IN (Position,Secondary)

Upvotes: 0

Related Questions