Undefined variable
Posted: Sat Sep 10, 2011 2:45 pm
I'm running into a problem where a file that I'm including that defines a variable is not being found. I am using this variable in a similar context for another file and it's finding it properly. Here's the flow:
Working version:
Uses Ajax to ping GetBrowseData.php which includes Connection.php. Connection.php has a variable $CardTable defined and GetBrowseData.php uses that correctly.
GetBrowseData.php
Connection.php
Now for the problem. My CreateState.php file includes CreateCard.php. This file is hit via a form in CreateState.php that does the following:
Then I respond to this by calling CreateCard() from my CreateState.php file. CreateCard.php includes Connection.php which *should* give me access to $CardTable, but instead gives me an "Undefined variable" notice. I'm at a loss as to why. Any ideas? Here's the relevant code:
CreateState.php
Pressing submit then sends the user back to the above file and calls CreateCard below.
CreateCard.php
Enums.php simply defines arrays such as $CardName, $CardColor, etc that are shared by many different files. There is no accidental duplication of $CardTable inside this file.
Does anyone know what might cause it to fail to find $CardTable in this instance?
Working version:
Uses Ajax to ping GetBrowseData.php which includes Connection.php. Connection.php has a variable $CardTable defined and GetBrowseData.php uses that correctly.
GetBrowseData.php
Code: Select all
<?php
include("Enums.php");
include("Connection.php");
$sqlConnection = OpenConnection();
mysql_query( $CardTable, $sqlConnection ) or mysql_error(); // Works, $CardTable is valid
...
?>
Code: Select all
<?php
$CardTableName = "MyTableName";
$CardTable = "CREATE TABLE ".$CardTableName." (
rowID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(rowID),
Name varchar(30)
)";
function OpenConnection()
{
$sqlConnection = mysql_connect( blah blah ); // Removed from this example for obvious reasons
if( !$sqlConnection )
{
die( 'Could not connect: ' . mysql_error() );
}
mysql_select_db( blah blah );
return $sqlConnection;
}
?>
Code: Select all
<form id="Data" action = "CreateState.php?userEntry=writeData" method = "post">
CreateState.php
Code: Select all
<html>
<body onload="OnLoad();">
<?php
include( "Enums.php" );
include( "Header.php" );
include( "CreateCard.php" );
?>
...
<?php
// User selected the CreateCard button
if( isset($_GET['userEntry']) && $_GET['userEntry'] == "writeData" )
{
CreateCard();
}
// New user
if( !isset($_GET['userEntry']) )
{
?>
<form id="Data" action = "CreateState.php?userEntry=writeData" method = "post">
...
// Above form calls it's own file with the writeData when they press submit.
CreateCard.php
Code: Select all
<?php
include( "Enums.php" );
include( "Connection.php" );
function CreateCard()
{
echo $CardTable; // Fails, gives me Notice: Undefined variable.
$sqlConnection = OpenConnection();
}
Does anyone know what might cause it to fail to find $CardTable in this instance?