# Another PHP Question - Global Variable?

**URL:** <https://boards.straightdope.com/t/another-php-question-global-variable/222242>\
**Category:** Factual Questions\
**Created:** [January 2, 2004, 6:24am UTC](https://boards.straightdope.com/t/another-php-question-global-variable/222242 "2004-01-02T06:24:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chuckster](https://avatars.discourse-cdn.com/v4/letter/c/b9bd4f/32.png) [@chuckster](https://boards.straightdope.com/u/chuckster)\
**Post date:** [January 2, 2004, 6:24am UTC](https://boards.straightdope.com/t/another-php-question-global-variable/222242/1 "2004-01-02T06:24:53Z")

</div>

I’m working on a website using HTML with embedded PHP. You guys were so helpful with my last question, I thought I’d ask you another. My website will be an online, searchable photo gallery. It allows the user to upload JPEG photos to a folder on the server and store information about that photo in an SQL database. I have one form to upload the JPEG photo an another form to upload the information on the photo. Problem is, if you don’t get the name of the photo exactly into the database as it appears in the folder, the link to the photo in the database will not find the photo in the folder. I would like to trap the name of the JPEG photo in the page that uploads the photo and copy it into the page that inserts the record into the database. I’m thinking a global variable is the best way to to this since both actions occur on different pages, and I might want to do other things with the image name on other pages as well.

My PHP code to upload the photo to the folder is as follows:  
$upfile = “/export/home/woodchuc/www/images/”.$userfile\_name;  
if (!copy($userfile, $upfile))  
{  
echo “Photo photo did not successfully copy into the IMAGES directory”;  
exit;  
}

echo “\<h3\>Photo Successfully Uploaded!\<br\>Click UPLOAD PIC INFO below to upload the photo information.\</h3\>”;

And, my PHP code to insert the record containing photo information into the SQL database is as follows:  
mysql\_select\_db(“bn65807”);  
echo “\<p\>Database Has Been Accessed!\</p\>”;

$query = “insert into bnsfloco (photo\_id, road\_number, locomotive\_type, paint\_scheme,  
photo\_month, photo\_day, photo\_year, location)  
values  
(’”.$photo\_id."’ , ‘".$road\_number."’, ‘".$locomotive\_type."’, ‘".$paint\_scheme."’,  
‘".$photo\_month."’, ‘".$photo\_day."’, ‘".$photo\_year."’, ‘".$location."’)";  
$result = mysql\_query($query);  
echo “\<h3\>”;  
echo "The information for your image “;  
echo $photo\_id;  
echo " has been successfully uploaded!”;

The variable $photo\_id is the record key variable I want to set to $userfile\_name. How would I do this using global variables, or is there a better way?

---

<div class="post-metadata">

**Author:** ![Bill\_H](https://avatars.discourse-cdn.com/v4/letter/b/a5b964/32.png) [@Bill\_H](https://boards.straightdope.com/u/Bill_H)\
**Post date:** [January 2, 2004, 7:31am UTC](https://boards.straightdope.com/t/another-php-question-global-variable/222242/2 "2004-01-02T07:31:14Z")

</div>

I believe you’re asking: “I have some text (an image name) in one form page, and I want to make use of it in one or more other pages that will immediately be accessed after this current one.” If that’s it, I recommend you just pass the image name to the next page as a hidden parameter, i.e. link to page2.php?image=xxx.jpg.

And, this will be the second time I shamelessly plug my [art](http://www.quoke.com) site within the hour, but it has many similar features to what you’ve got, and I’m happy to share my source with you if you’d like. Don’t have upload features though. That’s currently done manually.

---

<div class="post-metadata">

**Author:** ![Urban\_Ranger](https://avatars.discourse-cdn.com/v4/letter/u/e9c0ed/32.png) [@Urban\_Ranger](https://boards.straightdope.com/u/Urban_Ranger)\
**Post date:** [January 2, 2004, 7:35am UTC](https://boards.straightdope.com/t/another-php-question-global-variable/222242/3 "2004-01-02T07:35:09Z")

</div>

My suggestion is to merge the two forms, so you upload an image and the accompanying infomation at the same time.
