"if" problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

"if" problem

Post by davidjwest »

I'm new to PHP/SQL and have a simple problem - I've looked around various sites about if statements and I can't find the problem.

This bit of code is ignoring the if statements, it writes the variable $new_driver to all three fields when it should only do it on one field depending on what $new_drivers is set to.

Code: Select all

if ($new_drivers=1) {
$query = ("UPDATE teams SET driver_one='$new_driver_id' WHERE id='$ids'";)
mysql_query($query) or die (mysql_error());	
}	

if ($new_drivers=2) {
$query = ("UPDATE teams SET driver_two='$new_driver_id' WHERE id='$ids'");
mysql_query($query) or die (mysql_error());	
}	

if ($new_drivers=3) {
$query = ("UPDATE teams SET test_driver='$new_driver_id' WHERE id='$ids'");
mysql_query($query) or die (mysql_error());	
}
Any ideas?

I know the code is not very elegant but I'm learning!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Code: Select all

switch($new_drivers) {
case 1:
  $query = "UPDATE teams SET driver_one='$new_driver_id' WHERE id='$ids'";
  mysql_query($query) or die (mysql_error());
  break;
case 2:
  $query = "UPDATE teams SET driver_two='$new_driver_id' WHERE id='$ids'";
  mysql_query($query) or die (mysql_error());
  break;
case 3:
  $query = "UPDATE teams SET test_driver='$new_driver_id' WHERE id='$ids'";
  mysql_query($query) or die (mysql_error());
  break;
}
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Or, you could just use elseif statments. one note, your $query variable is wrong. Should be like:

Code: Select all

$query = "UPDATE teams SET driver_one='$new_driver_id' WHERE id='$ids'";
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

Hey, thanks very much, it worked!

I'm learning...........

:oops:

So why didn't my code work, explain in simple words only please!

:D
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

your if statement is using the assignment operator (=) instead of the equality comparison (==)
very common mistake that happens to all of us
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

so you end up with:

Code: Select all

switch($new_drivers)
{
   case '1':
      $query = ".........";
       break;

    case '2':
       $query = ".........";
       break;

     default:
        unset($query);
 }

if (isset($query))
{
        $result = mysql_query($query);
}
davidjwest
Forum Commoner
Posts: 67
Joined: Sat Nov 06, 2004 5:26 am
Location: Leeds, Yorkshire, England

Post by davidjwest »

andre_c wrote:your if statement is using the assignment operator (=) instead of the equality comparison (==)
very common mistake that happens to all of us
Ah!

See, I'm used to Spectrum Basic!

Thanks guys, if not for the help from these boards I'd have given up on my game long ago.

As it is I reckon I'm 10% or so through the coding.
Post Reply