3

I've got a weird problem: both my if and else statements are executing. Here's my code:

    if ($sel_user['name'] != $name) {
        $query = "UPDATE owner SET 
                ..."
        $result = mysql_query($query);
        if (mysql_affected_rows() ==1) {
            $query2 = "UPDATE queue_acl SET 
                ..."
            $result2 = mysql_query($query2);
            if (mysql_affected_rows() ==1) { 
                $_SESSION['updates_occurred'] = true;
            } else {
                $_SESSION['updates_occurred'] = false;
            }
        }
    }

    if ($sel_user['orgId'] != $orgId) {    
        $query = "UPDATE ownerOrganization SET
                ..."
        $result = mysql_query($query);
        if (mysql_affected_rows() ==1) {
            $query2 = "UPDATE queue_acl SET
                ..."
            $result2 = mysql_query($query2);
            if (mysql_affected_rows() ==1) {
                $_SESSION['updates_occurred'] = true;
            } else {
                $_SESSION['updates_occurred'] = false;
            }
        }
    }

    if ($sel_user['date_expires'] != $colVal[0] || 
            $sel_user['admin'] != $colVal[4]) {
        $query3 = "UPDATE queue_acl SET
                ..."
        $result3 = mysql_query($query3);
        if (mysql_affected_rows() ==1){
            $_SESSION['updates_occurred'] = true;
        } else {         
            $_SESSION['updates_occurred'] = false;
        }                
    } else {
        $_SESSION['updates_occurred'] = false;    
        $message = "<i>There were no edits to apply</i>";
    }

When I run this, the queries are being sent and everything is being updated fine, but the "There were no edits" message is also being printed

Anyone know why?

EDIT: I do not want to use elseif statements; the events are not mutually exclusive. That is, if $sel_user['name'] != $name AND $sel_user['orgId'] != $orgId, it is required that both queries are sent

3 Answers 3

8

If you don't want to wrap everything in an if/else if statements, one could set a flag at the end of each if check.

if($sel_user['name'] != $name) {
     // CODE HERE
     $flag = true;
}

if(!$flag){
     $message = "<i>There were no edits to apply</i>";
}

It's that or you can run the if check off $_SESSION['updates_occurred']

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Upon clarification of his question, it is clear the OP wants to run all three and report if none run.
That's a great suggestion -- fixed it. Thanks Tim :-)
This is really nice :)
8

Instead of doing lots of ifs, you should be doing elseifs

i.e.

if () {


} elseif () {


} else {

}

hope that helps.

3 Comments

Definitely the right advice. Factor your code in a way that these errors jump out at you. This is a good step. I suggest never writing in the } else { style. You want your if and paired else if and else indented at the same level.
Thanks for the suggestion, but that is not what I want. I did not use elseif statements because I do not want the updates to be mutually exclusive; more than one statement can be executed
Hi eigen, looks like Tim's got you sorted but it does make this question rather misleading.
0
if(!printf("Hello")) {
    echo "Hello";
}
else {
    echo " World";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.