Jump to content

Recommended Posts

Hello :-)

 

I am trying to get only one PackageImages object to persist across page refreshes ( a singleton ).

 

I think the problem lies with my (mis)understanding Sessions in PHP.

 

Can anyone help clarify?

 

<?php

require_once("../../classes/database/databaseConnector.php");

session_start;

class PackageImages {

var $databaseConnector;

var $locationId;
var $brandId;
var $packageId;

function PackageImages() {

	$this->databaseConnector = & new DatabaseConnector();

}

function renderLocationOptions() {

	$sqlLocations = $this->databaseConnector->selectQuery("SELECT locationId,locationName FROM Locations");

	if ( mysql_num_rows( $sqlLocations ) > 0 ) {

		while( $location = mysql_fetch_array( $sqlLocations ) ) {

			echo('<option value="'.$location['locationId'].'">'.$location['locationName'].'</option>');

		}

	} else {

		echo('PackageImages->renderLocationOptions() $sqlLocations Error : No records found!');

	}

}

function renderBrandOptions() {

	$sqlBrands = $this->databaseConnector->selectQuery("SELECT id, brandName FROM Brands");

	if ( mysql_num_rows( $sqlBrands ) > 0 ) {

		while( $brand = mysql_fetch_array( $sqlBrands ) ) {

			if ( $brand['brandName'] != 'www.escapeltd.co.uk' ) {

				echo('<option value="'.$brand['id'].'">'.$brand['brandName'].'</option>');

			}

		}

	} else {

		echo('PackageImages->renderBrandOptions() $sqlBrands Error : No records found!');

	}

}

function renderPackageOptions() {

	$locationId = $this->locationId;
	$brandId = $this->brandId;

	$sqlPackages = $this->databaseConnector->selectQuery("SELECT Packages.id, Packages.packageTitle 
														FROM Packages 
														WHERE Packages.id IN 
															(SELECT PackageLocations.packageId 
															 FROM PackageLocations 
															 JOIN PackageBrands 
															 ON PackageLocations.packageId = PackageBrands.packageId 
															 WHERE PackageLocations.locationId = '$locationId' 
															 AND PackageBrands.brandId = '$brandId') 
														ORDER BY Packages.packageTitle ASC");

	if ( mysql_num_rows( $sqlPackages ) > 0 ) {

		while( $package = mysql_fetch_array( $sqlPackages ) ) {

			echo( '<option value="'.$package['packageId'].'">'.$package['packageTitle'].'</option>');

		}

	} else {

		echo('PackageImages->renderPackageOptions $sqlPackages error : No records found!<br/>');

	}

}

function catchPostData() {

	if ( $_POST['formTag'] == 'packagesFilter' ) {

		$this->locationId = $_POST['location'];
		$this->brandId = $_POST['brand'];

	}

	$this->packageId = $_POST['packageId'];

}

}

if ( !isset( $_SESSION['packageImages'] ) ) {

$packageImages = & new PackageImages();
$_SESSION['packageImages'] = $packageImages;

} else {

$packageImages = $_SESSION['packageImages'];

}

$packageImages->catchPostData();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Manage Package Images</title>
</head>

<body>
<?php
echo('LocationId : '.$packageImages->locationId.'<br/>');
echo('BrandId : '.$packageImages->brandId.'<br/>');
echo('Package Id : '.$packageImages->packageId.'<br/>');
?>
<a href="../index.php">Escape Limited Management</a>

<fieldset>
<legend>Packages Filter</legend>
	<form name="packagesFilter" action="<?php echo( $_SERVER['PHP_SELF'] ); ?>" method="post">
		<input type="hidden" name="formTag" value="packagesFilter"/>
		<label for="location">Location</label>
		<select name="location" size="1">
			<?php $packageImages->renderLocationOptions(); ?>
		</select>
		<label for="brand">Brand</label>
		<select name="brand" size="1">
			<?php $packageImages->renderBrandOptions(); ?>
		</select>
		<input type="submit" value="Submit"/>
	</form>
</fieldset>

<fieldset>
<legend>Package Selector</legend>
	<form name="packageSelector" action="<?php echo( $_SERVER['PHP_SELF'] ); ?>" method="post">
		<label for="package">Package</label>
		<select name="package" size="1">
			<?php $packageImages->renderPackageOptions(); ?>
		</select>
		<input type="submit" value="Submit"/>
	</form>
</fieldset>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/51308-session-problem/
Share on other sites

It is in require_once("../../classes/database/databaseConnector.php");

 

<?php

session_start();

class DatabaseConnector {

var $host;
var $username;
var $password;
var $database;

var $lastInsertId;

function DatabaseConnector() {
//			echo('DatabaseConnector->constructor()<br/>');

	$this->host = "localhost";
	$this->username = "username";
	$this->password = "password";
	$this->database = "database";

	$databaseConnection = mysql_connect( $this->host, $this->username, $this->password ) or die ('Unable to connect to database because: '.mysql_error() );		
	mysql_select_db( $this->database, $databaseConnection ) or die('Unable to select database because '.mysql_error() );

}

function getLastInsertId() {

	return $this->lastInsertId;

}

// Returns a mysql result
function insertQuery( $query ) {
//			echo('DatabaseConnector->insertQuery()<br/>');

	$sql = mysql_query( $query ) or die( mysql_error() );

	$this->lastInsertId = mysql_insert_id();

	return $sql;

}

// Returns a mysql result
function selectQuery( $query ) {
//			echo('DatabaseConnector->selectQuery()<br/>');

	$sql = mysql_query( $query ) or die( mysql_error() );

	return $sql;

}

// Returns a mysql result
function updateQuery( $query ) {
//			echo('DatabaseConnector->selectQuery()<br/>');

	$sql = mysql_query( $query ) or die( mysql_error() );

	return $sql;

}

// Performs a delete query
function deleteQuery( $query ) {
//			echo('DatabaseConnector->deleteQuery()<br/>');

	mysql_query( $query ) or die( mysql_error() );

}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/51308-session-problem/#findComment-252748
Share on other sites

Hello :-)

 

Sorry for the delay - I had to sleep!

 

The error message is as follows:

<b>Warning</b>:  mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: Access denied for user 'c20esca'@'localhost' (using password: NO) in <b>/home/c20esca/public_html/classes/database/databaseConnector.php</b> on line <b>49</b><br />

<br />
<b>Warning</b>:  mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: A link to the server could not be established in <b>/home/c20esca/public_html/classes/database/databaseConnector.php</b> on line <b>49</b><br />
Access denied for user 'c20esca'@'localhost' (using password: NO)

 

Thanks for your help so far.

Link to comment
https://forums.phpfreaks.com/topic/51308-session-problem/#findComment-253416
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.