nv39
nv39

Reputation: 535

both if and else statements excecuting

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

Upvotes: 3

Views: 1868

Answers (3)

Khushal Singh Dangwal
Khushal Singh Dangwal

Reputation: 21

if(!printf("Hello")) {
    echo "Hello";
}
else {
    echo " World";
}

Upvotes: 0

Telshin
Telshin

Reputation: 340

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']

Upvotes: 8

Rob Forrest
Rob Forrest

Reputation: 7450

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

i.e.

if () {


} elseif () {


} else {

}

hope that helps.

Upvotes: 8

Related Questions