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

Link to comment
Share on other sites

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

Link to comment
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_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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.... :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.