Reputation: 195
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);
}
?>
Upvotes: 0
Views: 56
Reputation: 1876
The previous solutions don't address the problem.
What you need to use is the if-else block after if.
if(conditionA is true){
//do taskA
}
else if(conditionB is true){
//do taskB
}
else if(condition is true){
//do taskC
}
else {
//do taskD
}
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,
if($value == "Paid" || $value == "Waiting on Payment"){
$sql = "UPDATE test SET payment='$value' WHERE id='$id'";
}
else if($value == "Repaired" || $value == "Waiting on Repairs"){
$sql = "UPDATE test SET repair='$value' WHERE id='$id'";
}
else if($value == "With Student" || $value == "Awaiting Pickup"){
$sql = "UPDATE test SET returned='$value' WHERE id='$id'";
}
Upvotes: 1
Reputation: 5857
i know this is off topic but is there any other way to accomplish this without using 3 individual if statements? – Matthew
switch($value)
{
case "Paid":
case "Waiting on Payment":
$column = "payment";
break;
case "Repaired":
case "Waiting on Repairs":
$column = "repair";
break;
case "With Student":
case "Awaiting Pickup":
$column = "returned";
break;
default:
die("TODO: Error handling for unknown value: ".$value);
}
$sql = "UPDATE test SET ".$column." = '".$value."' WHERE id='".$id."'";
Upvotes: 0
Reputation:
if($value == "Paid" || "Waiting on Payment"){
should be
if($value == "Paid" || $value =="Waiting on Payment"){
SWITCH version
switch ($value) {
case 'Paid':
case 'Waiting on Payment'
$sql = "UPDATE test SET payment='$value' WHERE id='$id'";
break;
case 'Repaired':
case 'Waiting on Repairs':
$sql = "UPDATE test SET repair='$value' WHERE id='$id'";
break;
case 'With Student':
case "Awaiting Pickup":
$sql = "UPDATE test SET returned='$value' WHERE id='$id'";
break;
}
Upvotes: 3