Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 28th, 2007, 09:37 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem accessing global variable inside functions

Hey everyone:

I'm having an issue that I have been troubleshooting for literally two days and I just can't seem to figure it out. If anybody can help me understand why this isn't working, I'd owe you big time. I'll try to keep it as brief as possible.

I have an html page which uses a form to collect data. Here is a sample of the code:

<form action="New.php" method="post">
Please enter your first name<br><br>
First name:<input type="Text" name="firstinq"><br>
<input name="submit" type="Submit" value="Submit"><br>

So, here I understand it's going to pass the variable firstinq to a page named New.php.

Inside this New.php file, I take the firstinq variable (through use of $_POST) and put it into a new variable:

<?php
$FirstNameGiven = $_POST["firstinq"];

Now if at this point in the code, I do:
echo $FirstNameGiven;
I am happy with what comes out- the name that the user inputted on the previous screen.

However later in the script I have a function- that I just cannot get to have any type of access to $FirstNameGiven, seemingly no matter what I try:

<?php
function Updatedata ($fieldbeingchanged,$thevalue)
{
global $FirstNameGiven;
mysql_query("UPDATE inquirydatabase SET $fieldbeingchanged = '$thevalue' WHERE first='$FirstNameGiven'");
}
?>

I wrote a function that has two arguments- the first is going to be indicating which field in the database needs to be changed, and then the second will have the replacement value. I know that the arguments are working fine because if I echo both of these inside of the function, the correct values are shown. However, when I check the database, there is no change made to the MYSQL database. And, if I echo $FirstNameGiven in the function, nothing is returned. In the mysql_query above, if I replace $FirstNameGiven at the end and just manually put in the name I typed in on the previous screen, I go back to the database and it is changed correctly. So, I know that everything else is working. It just seems as though the function isn’t accessing $FirstNameGiven. But...I'm just not sure what to do. I declared it as a global variable inside of the function and every site I seem to find that talks about this situation says that that is all you have to do. I really do not want to pass the variable as another argument in the Updatedata function.

I've been at this for countless hours. It seems like I truly tried every combination of everything- so I'm thinking that there is just some sort of rule that I do not understand.

Any and all help would be GREATLY appreciated.

Thanks in advance-
Jeff


Reply With Quote
  #2 (permalink)  
Old June 29th, 2007, 08:50 AM
Friend of Wrox
 
Join Date: Nov 2005
Location: Rotterdam, , Netherlands.
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

weird it seems that your code works fine on my computer try this instead
now you wont use the global and you just pass it in the function value maybe it has to do with your php version
Code:
function Updatedata ($fieldbeingchanged,$thevalue,$FirstNameGiven)
{

    mysql_query("UPDATE inquirydatabase SET $fieldbeingchanged = '$thevalue' WHERE first='$FirstNameGiven'");
}
Or
Code:
function Updatedata ($fieldbeingchanged,$thevalue)
{
    $FirstNameGiven = $_POST["firstinq"];
    mysql_query("UPDATE inquirydatabase SET $fieldbeingchanged = '$thevalue' WHERE first='$FirstNameGiven'");
}
__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
Reply With Quote
  #3 (permalink)  
Old June 29th, 2007, 12:41 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 weird it seems that your code works fine on my computer try this instead

You have no idea- and I'll tell you why I can't even do it the way you are describing- it gets weirder....

And thank you so much for offering to help with this- I need to get this up and running- it's not just for my own personal fun- and so I'm not sure where else to turn.

For example let's say I have the following code:

<form action="New.php" method="post">
Please enter your first name<br><br>
First name:<input type="Text" name="firstinq"><br>
<input name="submit" type="Submit" value="Submit"><br>

That is the original page- on the New.php page... here is the extra issue:

I have this code:

<?php
$FirstNameGiven = $_POST["firstinq"];

This takes the form entry on the first page and puts it into $FirstNameGiven. Cool.

Let say I do this:

<html>
<body>
<?php
$FirstNameGiven = $_POST["firstinq"];
echo $FirstNameGiven;
?>
</body>
</html>

This works great! Hurray. The echo returns the appropriate value.

Now check this:

<html>
<body>
<?php

$FirstNameGiven = $_POST["firstinq"];
echo $FirstNameGiven;

if($somevalueofsomevariable)
{
echo "If you see this, this if statement is being called";
echo $FirstNameGiven;
}
?>

</body>
</html>

It returns: If you see this, this if statement is being called.
... and absolutely nothing else.

The IF statement is not returning any value for $FirstNameGiven

If I echo it BEFORE the if statement, it does, but INSIDE the IF statement... it doesn't. I've tried declaring the variable as global inside the if statement but that doesn't work either.

So I can't even use your suggestion of passing the $FirstNameGiven as an argument in the Function, but the function is called several times, IN the if statement.....

So I can't even find a work around for it.

Here is the full code of the page- I watered it down as much as possible to make it easy to understand- I put comments in with ***comment***


<html>
<body>
<form method="post" action="<?php echo $PHP_SELF?>">

<?php

$FirstNameGiven = $_POST["firstinq"]; *** takes value from form on previous page and assigns it to $FirstNameGiven
echo $FirstNameGiven; ***Returns the correct value this is the ONLY place in the code where it works***

$db=mysql_connect(blahblah);
mysql_select_db("blah",$db);

   $result = mysql_query("SELECT * FROM blah WHERE first='$FirstNameGiven'",$db);
   $myrow = mysql_fetch_array($result);

function Updatedata ($fieldbeingchanged,$thevalue)
{
global $FirstNameGiven;
echo $FirstNameGiven; ***DOES NOT WORK-- RETURNS NO VALUE***
mysql_query("UPDATE blah SET $fieldbeingchanged = '$thevalue' WHERE first='$FirstNameGiven'");
}

if ($finished) ***$finished comes when the user hits the submit button from the html form below***
{
echo $FirstNameGiven;
***DOES NOT WORK-- RETURNS NO VALUE-- because of this I can't feed the value to the function as an argument in the next line***

Updatedata ("first",$first);
***"first" name of the field in database, $first- obtained from HTML form on this page
}

else
{
?>

Please make the changes to the database entry and hit submit at the bottom. <br><br><br>

<input type="Text" name="first"><br>
<input type="Submit" name="finished" value="Submit">

<?
}
?>
</body>
</html>




Any ideas?


Reply With Quote
  #4 (permalink)  
Old June 29th, 2007, 12:51 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As for the information about the version of php, my webserver says this:

PHP5 is available on all of our hosting servers.

By default, files with the .php5 extension will be parsed by the PHP 5 interpreter, while .php files will be parsed by the PHP 4 interpreter.

If you prefer to have .php files parsed by the PHP 5 interpreter, you can add the following line in a .htaccess file:

I name my files with a '.php' extension, so I guess then it is being interpreted as PHP version 4.

I don't know if that helps..

- Jeff

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Problem accessing Context Variable vayumahesh JSP Basics 1 August 5th, 2006 10:28 AM
Having problem inside functions (seg-fault). mgm BOOK: Professional Assembly Language 0 August 4th, 2006 12:34 PM
Global variables and functions madhukp ASP.NET 1.x and 2.0 Application Design 3 October 9th, 2003 09:57 AM



All times are GMT -4. The time now is 03:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.