Page 1 of 1

PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 6:19 pm
by cloudbase
Hello Everyone. I really try to figure things out before asking. Please HELP I'm into this 2 days now.
I've abbreviated the entire code to what must be relevant replicating the exact error from the full program.
Here it is with the issue commented on line #24

Code: Select all

<?php
	function renderform($notice)	{
?>

<html>
<head>
<title></title>
</head>
<body>
<div id="container">
		
<?php
	if($notice != '')	{
		echo '<div style="padding:4px; border:1px solid red; color:red;">' . $notice . '</div>';
		unset($notice);
	}
?> 

<form action= "" method="post">
	<input type="radio" name="filter" id="filter" value="flyin" /> Flyin

<?php
	include_once 'Connect_Db.php'; 
	/* * * Above include works fine when page loads, on Submit it fails with following message:
			Undefined variable: MemberDbTableName in C:\wamp\www\public_html\BmfMemberDbFiles\debug.php on line 43
			The below is the contents of 'Connect_Db.php'
			When I run it instead of above include_once 'Connect_Db.php';
			It works without fail everytime all day long.

			$server = 'localhost';
			$user = 'user';
			$pass = 'password';
			$db = 'test';
			$MemberDbTableName = 'members_beta';
			$connection = mysql_connect($server, $user, $pass) 
			or die ("Could not connect to server ... \n" . mysql_error ());
			mysql_select_db($db) 
			or die ("Could not connect to database ... \n" . mysql_error ());
	*/									
	$result = mysql_query("SELECT * 
									FROM $MemberDbTableName 
									WHERE type='flyin' 
									GROUP BY EXTRACT(YEAR FROM dues_paid)	") or die($result."<br/><br/>".mysql_error());
	if(is_resource($result))	{
		echo '<select name="select_flyin_year">';
		while($row = mysql_fetch_array($result))	{
			$select_flyin_year = date('Y', strtotime($row['dues_paid']));
			echo '<option value="' . $select_flyin_year . '">' . $select_flyin_year;
		}
		echo '</select>';
	}
?>

	<br />
	<input type="radio" name="filter" id="filter" value="all" /> All<br />
	<textarea rows="12" cols="70" name="message" id="message"></textarea>
	<input type="submit" name="submit" value="Submit" />
</form>
</div>
</body>
</html>

<?php
}
	if (isset($_POST['submit']))	{	
		if (!empty($_POST['message']))	{	//do some stuff
		}
		else	{
			$error = "Check form for Error!";
			renderform($error);
		}
		if (!isset($error)) {
			include_once 'Connect_Db.php';
			//perform query
			renderform('success');
		}
	}
	else	{	//the form hasn't been submitted
		renderform('');
	}
			
?>

Re: PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 7:45 pm
by califdon
Notice that the error message says it found the error on line 43 of a file named "debug.php" -- is that the name of the file you are showing here? And how are you "reloading" the page, with the browser refresh button?

Re: PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 8:03 pm
by cloudbase
califdon, sorry-
Yes the file above is debug.php that i'm showing.
This is the mysql_error message
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE type='flyin' GROUP BY EXTRACT(YEAR FROM dues_paid)' at line 3
Which puts it, in my experience, at FROM $MemberDbTableName the same variable php say's is undefined.
Using the html Submit button line 57

Re: PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 8:30 pm
by califdon
OK, I think I now see what you're doing. The first time you load the page, you get no error, right? Your form is blank, then when the user fills in the form and submits, its action= is blank, so it calls the same script again, and this time you get the error. Is that what happens? I think what is going on is that since you are using include_once, the second time the script is executed in the same user session, it doesn't include the file again, and it's in that file that you declare the variable. The database connection remains valid during the session, but not variables, whose scope is the script they are declared in. You should not be declaring your table name variable in the include file. Do that in the main script. Try that, I think it will clear up the problem.

Re: PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 8:47 pm
by cloudbase
You are SOOOOOOO! right
Just for a test I added $MemberDbTableName = 'members_beta'; after include_once 'Connect_Db.php';
and it's working.

My reason for $MemberDbTableName declared in Connect_Db.php is the php code runs for many websites and I only need to change Connect_Db.php when I have a new client.

Thanks Very much

Ron

Re: PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 9:22 pm
by cloudbase
Again Thanks califdon.
To wrap this up with a work-around that works for me I'm just sending $MemberDbTableName along with the success to renderform().

2 code changes later
function renderform($notice, $MemberDbTableName)
renderform('success', $MemberDbTableName);

priceless.........

Re: PHP mysql_query fails after page reload

Posted: Mon Jun 18, 2012 11:09 pm
by califdon
cloudbase wrote:My reason for $MemberDbTableName declared in Connect_Db.php is the php code runs for many websites and I only need to change Connect_Db.php when I have a new client.
You couid have another php file that contains nothing but the one line assigning the value to the variable, then include (not include_once) that file in your script. Or another way would be to just read the content of a text file containing just the name of the table. Your script could then have a line like this:

Code: Select all

$tablename = file_get_contents("tablename.txt");
Then you could just supply the identical php script file and a one-line custom text file to each client.