Page 1 of 1

variable scope problem

Posted: Mon Oct 22, 2007 9:07 am
by ashrafzia
I want the value of the variable $user to go inside the if statement inside the query, but its not working.
How can i make the value of the variable $user as a global for access inside if....else block statements.

Code: Select all

$val = $_GET['id'];
$user = $_GET['user'];

if ($val == "AddPaper" && !isset($_POST['next_x'])){

//echo "$user";

$sql="SELECT programe_name FROM teachers WHERE teacher_name='$user' ";

$result = mysql_query($sql, $conn) or die ("Can't Process Query".mysql_query());
while ($row=mysql_fetch_array($result)){
		$add1 .= "<option value='$row[programe_name]'>$row[programe_name]</option>";
}

I have a dropdown menu which has a link as http://localhost/xampp/......../myfile?id=AddPaper
when i click the button the above if statement is executed but the value of the variable $user isn't coming inside it and before the button click i have a value inside the link as http://localhost/xampp.......myfile?user=$_post[name].

I hope you get what i want to say.

Posted: Mon Oct 22, 2007 9:09 am
by aaronhall
if blocks don't maintain their own scope like functions do. $user will be visible inside that block. Check that the if conditions are evaluating to true.

Posted: Mon Oct 22, 2007 9:14 am
by ashrafzia
aaronhall wrote:if blocks don't maintain their own scope like functions do. $user will be visible inside that block. Check that the if conditions are evaluating to true.
I have checked the if condition are evaluating to true but the value of the variable isn't printing.
I have echoed it. Above if statement its working, but inside if statement its not working.

Posted: Mon Oct 22, 2007 9:44 am
by aaronhall
Place this line directly above the if statement:

Code: Select all

var_dump($val == "AddPaper" && !isset($_POST['next_x']);
If it doesn't print true, the statement isn't true

Posted: Mon Oct 22, 2007 12:46 pm
by ashrafzia
aaronhall wrote:Place this line directly above the if statement:

Code: Select all

var_dump($val == "AddPaper" && !isset($_POST['next_x']);
If it doesn't print true, the statement isn't true
It's giving a parse error and will you kindly explain what is it used for ?

Posted: Mon Oct 22, 2007 1:06 pm
by John Cartwright
ashrafzia wrote:
aaronhall wrote:Place this line directly above the if statement:

Code: Select all

var_dump($val == "AddPaper" && !isset($_POST['next_x']);
If it doesn't print true, the statement isn't true
It's giving a parse error and will you kindly explain what is it used for ?
It's missing a closing bracket.

Try var_dump()'ing all the variables separately to get a better picture.

Code: Select all

echo 'Val: '; var_dump($val); echo '<br />';
echo '$_POST[\'next_x\']: '; var_dump($_POST['next_x']); echo '<br />';
echo 'if(): '; var_dump($val == "AddPaper" && !isset($_POST['next_x']));

Posted: Mon Oct 22, 2007 1:24 pm
by ashrafzia
Jcart wrote:
It's missing a closing bracket.

Try var_dump()'ing all the variables separately to get a better picture.

Code: Select all

echo 'Val: '; var_dump($val); echo '<br />';
echo '$_POST[\'next_x\']: '; var_dump($_POST['next_x']); echo '<br />';
echo 'if(): '; var_dump($val == "AddPaper" && !isset($_POST['next_x']));
Its giving the following output:

Code: Select all

Val: NULL 
$_POST['next_x']: NULL 
if(): bool(false)