8

I am trying to do a check before the data inserts into the MySQL query. Here is the code;

$userid = ($vbulletin->userinfo['userid']);
$sql3 = mysql_query("SELECT * FROM table WHERE ID='$_POST[hiddenID]'");

while ($row = mysql_fetch_array($sql3)){

$toon = $row['toonname'];
$laff = $row['tlaff'];
$type = $row['ttype'];

if ($type == 1){
$type == "Bear";
} elseif ($type == 2){
$type == "Cat";
} elseif ($type == 3){
$type == "Dog";
}            

}

However, this isn't working. Basically, there are different values in the 'table' for each type. 1 means Bear, 2 means Cat, and 3 means Dog.

Thanks to whomever can help see a problem in my script!

llw
  • 201
  • 1
  • 3
  • 15

4 Answers4

20

You are comparing, not assigning:

if ($type == 1){
  $type = "Bear"; 
}

You compare values with == or ===.

You assign values with =.

You could write less code to achieve the same result too, with a switch statement, or just a bunch of ifs without the elseifs.

if ($type == 1) $type = "Bear";
if ($type == 2) $type = "Cat";
if ($type == 3) $type = "Dog";

I would make a function for it, like this:

function get_species($type) {
    switch ($type):
        case 1: return 'Bear';
        case 2: return 'Cat';
        case 3: return 'Dog';
       default: return 'Jeff Atwood';
    endswitch;
}

$type = get_species($row['ttype']);
Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
3

You are using == instead of =. It compares the variable to the new value. Use = to set the value.

if ($type == 1){
$type = "Bear";
} elseif ($type == 2){
$type = "Cat";
} elseif ($type == 3){
$type = "Dog";
}  
PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
2

You're using == to assign values:

$type == bear;

Should be:

$type = bear;

LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
0
if ($type == 1) {$displayVar = "Bear";}

Example:

<form method="post" action="results.php">
How many horns does a unicorn have? <br />
<input type="text" name="inputField" id="inputField" /> <br />
<input type="submit" value="Submit" /> <br />
</form>

Results:

<?php 
$inputVar = $_POST["inputField"];
if ($inputVar == 1) {$answerVar = "correct";}
else $answerVar = "<strong>not correct</strong>";
?>
<?php 
echo "Your answer is " . $answerVar . "<br />";
?>
Porta Shqipe
  • 766
  • 7
  • 8