Jump to content

variable scope problem


ashrafzia

Recommended Posts

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.

 

$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.

Link to comment
https://forums.phpfreaks.com/topic/74313-variable-scope-problem/
Share on other sites

Well for starters, you will probably be getting an error because of this line:

 

$result = mysql_query($sql, $conn) or die ("Can't Process Query".mysql_query());

 

It should be:

 

$result = mysql_query($sql, $conn) or die ("Can't Process Query".mysql_error());

 

And im a little confused by your question. Are both $id and $name defined when you attempt this query? Im unsure as to what the drop down box has to do with this all.

$user = $_GET['user'];

 

is $_GET['user'] defined in the URL?

 

i.e.. page.php?user=Fred

 

Yes it is defined.

I have an authentication script and after authenticating a person i am redirecting him to another page through header function and passing his name like this:

header("Location: http://localhost/xampp/mypic/pictures.php?user=$_POST[name] ");

Then i am getting his name like this:

$user = $_GET['user'];

Its working fine uptil here.

Now when the if....else block statement appears it is hush. I don't know why?

Well for starters, you will probably be getting an error because of this line:

 

$result = mysql_query($sql, $conn) or die ("Can't Process Query".mysql_error());

 

And im a little confused by your question. Are both $id and $name defined when you attempt this query? Im unsure as to what the drop down box has to do with this all.

 

Nice mistake pointed out. I didn't realized.

Yes, both of them are defined.

Well, if you are transferring with this header:

 

header("Location: http://localhost/xampp/mypic/pictures.php?user=$_POST[name] ");

 

Then they are not both defined. $id would not be defined, so the if statement would evaluate to false.

 

You would need to do somthing like:

 

header("Location: http://localhost/xampp/mypic/pictures.php?user=$_POST[name]&id=somevalue");

Well, if you are transferring with this header:

 

header("Location: http://localhost/xampp/mypic/pictures.php?user=$_POST[name] ");

 

Then they are not both defined. $id would not be defined, so the if statement would evaluate to false.

 

You would need to do somthing like:

 

header("Location: http://localhost/xampp/mypic/pictures.php?user=$_POST[name]&id=somevalue");

 

 

Still not working.... :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.