I have these 3 if statements which depending on the value in the variable will either put it into one column or another. But for some reason it always uses only one of the 3 even if the if statement should be false. I have placed the code below. Thanks for any help.
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
// error happened
print(0);
}
mysql_select_db('test');
// sanitize the value
$value = mysql_real_escape_string($_GET['uname']);
$id = mysql_real_escape_string($_GET['id']);
if($value == "Paid" || "Waiting on Payment"){
$sql = "UPDATE test SET payment='$value' WHERE id='$id'";
}
if($value == "Repaired" || "Waiting on Repairs"){
$sql = "UPDATE test SET repair='$value' WHERE id='$id'";
}
if($value == "With Student" || "Awaiting Pickup"){ //Always uses this one for somereason unknown to me..
$sql = "UPDATE test SET returned='$value' WHERE id='$id'";
}
// start the query
// check if the query was executed
if(mysql_query($sql, $link)){
// everything is Ok, the data was inserted
print(1);
echo $sql;
} else {
// error happened
print(0);
}
?>
The previous solutions don’t address the problem.
What you need to use is the if-else block after if.
That answers your question.
But then comes the next problem, which is answered @ https://stackoverflow.com/a/10492151/260034 by “A Bag”. It is because the strings “Waiting on Payment”, “Waiting on Repairs”, etc always get evaluated to TRUE. So even if $value is not equal to “Paid”, the string “Waiting on Payment” evaluates to TRUE.
The OR operator || returns TRUE if either of the 2 operands evaluates to TRUE.
So all your if statements evaluate to TRUE and since you have a bunch of if statements and not an if-ifelse block, the block of code in your last if condition seems to be the one which is always executing.
The revised code would be something like,