Changing global variable value in if condition, then accessing the value in else
I have a global variable being changed in if statement then accessed in else, but it does not have the same value and loses its value:
global $reqUserID;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['viewReq'])) {
开发者_如何学C $requests = new requests();
$results = $requests->getRequest($_POST['reqID']);
$result = mysql_fetch_array($results, MYSQL_ASSOC))
$GLOBALS['reqUserID'] = $result['USER_ID']; //CORRECT VALUE HERE
}
else if (isset($_POST['approveBtn'])) {
$var1 = $GLOBALS['reqUserID'];
$sql = "SELECT EMAIL FROM user_info WHERE USER_ID='$var1'";//SOME OTHER VALUE HERE
[...]
}
If you set a value in the 'if' clause, then the 'else' clause will not be executed, and vice versa. So, you could only get to the 'else' clause on a second iteration of the statement. Then you need to understand where variables are defined, and when they become undefined.
In one pass over the if-else construct you can either enter in the if-construct or the else construct. So, the change you make in if-part will not be visible in the code in else-if construct.
page was being posted to itself, and Global variable involved was not being posted to be used, so i made a hidden field filled it with the value of global variable on the first pass and posted the value and used it late using _POST array.
精彩评论