Page 1 of 1

"if" problem

Posted: Fri Dec 17, 2004 4:27 pm
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!

Posted: Fri Dec 17, 2004 4:30 pm
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;
}

Posted: Fri Dec 17, 2004 4:33 pm
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'";

Posted: Fri Dec 17, 2004 4:58 pm
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

Posted: Fri Dec 17, 2004 5:47 pm
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

Posted: Fri Dec 17, 2004 6:15 pm
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);
}

Posted: Sat Dec 18, 2004 2:35 pm
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.