Session is only grabbing one variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Session is only grabbing one variable

Post by SirChick »

I have session which makes the variables yet is only takes 1 of the 3 across pages. Its very odd. Especially "price". As price is formed in the exact same manner as "housetype" for some unknown reason it won't store it like it does for "housetype"

Heres the code involved.

List box to display what the user may want to buy, these 2 scripts are on the same page! :

Code: Select all

<form name="Form1" method="POST" action="housepurchaseprocess.php" enctype="multipart/form-data" id="Form1" onsubmit="return ValidateForm1(this)">

<?php
$soldhousesquery = "SELECT Price, HouseType FROM soldhouses ORDER BY Price ASC";
$soldhousesresult = @mysql_query($soldhousesquery) or die(mysql_error());


echo '<select name="houselist" size=10>">';

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value="{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}

echo '</select>';

?>
</div>
<input type="submit" id="Button2" name="Buy" value="Buy House" style="position:absolute;left:350px;top:600px;width:184px;height:24px;z-index:20">
</form>

Code: Select all

<?
include ("include.php");
include ("energybarinclude.php");


session_start();
$_SESSION['Price'] = $soldhousesrow['Price'];
$_SESSION['HouseID'] = $soldhousesrow['SoldHouseID'];
$_SESSION['NumberOfRooms'] = $soldhousesrow['NumberOfRooms'];
$_SESSION['HouseType'] = $soldhousesrow['HouseType'];
?>

Process page:

Code: Select all

<?
include ("include.php");
session_start();


$FindCityID = mysql_query("SELECT * FROM userregistration
                    WHERE UserID='{$_SESSION['Current_User']}'");
// Fetch the row from the database
if (!($row = mysql_fetch_assoc($FindCityID))) {
    echo "City ID not found!";
    exit;
	echo mysql_error();
}


$Area = $_SESSION['Area'];
$Street = $_SESSION['Streetname'];
$HouseID = $_SESSION['SoldHouseID'];
$NumberOfRooms = $_SESSION['NumberOfRooms'];
$Price = $_SESSION['Price'];
$CityID = $row['CityID'];
$HouseType = $_SESSION['HouseType'];
$UserID = $_SESSION['Current_User'];
$Money = $row['MoneyInHand'];
Echo $CityID;
Echo $Price;
Echo '</br>';
print_r ($Price);
Echo '</br>';
Echo $HouseType;
Echo '</br>';
Echo $UserID;
Echo '</br>';
Echo $Money;
Echo '</br>';
Echo $HouseID;
Echo '</br>';
Echo $NumberOfRooms;
Echo '</br>';
Echo $Area;
Echo '</br>';
Echo $Street;
Echo '</br>';

If ($Money > $Price) {
			$subtractMoney = "UPDATE userregistration SET MoneyInHand=MoneyInHand-$price WHERE UserID='{$_SESSION['Current_User']}'";
			$result = mysql_query($addMoney) or die(mysql_error());
			if (mysql_affected_rows() == 0)
			{
			die('Error ID 0001A, contact admin on the Civilian forums immediately about this error!');
			}


{
  $val = explode('|', $_POST['houselist']);
  if (count($val))
  {
    $price = $val[0];
    $houseType = $val[1];
  }
}

$boughthouse = "INSERT INTO `houses` (CityID,HouseType,Price,StreetName,Area,UserID)
               Values ('$CityID', '$HouseType', '$price', '$Street', '$Area', '$UserID')";
      mysql_query($boughthouse) or die(mysql_error());
$Deletesoldhouses = "DELETE * FROM soldhouses
					WHERE SoldHouseID = $HouseID";
				mysql_query($Deletesoldhouses) or die(mysql_error());	
					

			header("Location: choosehousenumber.php");
}
Else {
Echo 'You do not have enough money, i guess you will have to go to a mortgage company!';
	}  
?>

The large list of echo's is just to test the values,, "housetype" echo's fine but yet "price" does not .. nor does the $HouseID;
$price;
$NumberOfRooms;

Im finding it hard to work out why [s]cos[/s] because price especially is coming from same place as housetype but wont work yet housetype does... its deffinatly filled in table and its spelt correctly.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Session is only grabbing one variable

Post by volka »

SirChick wrote:these 2 scripts are on the same page!
exactly where is that second code snippet located?
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

above the first script i gave ya.


i put them in a silly order in here :P but yeh its above the option list... theres html that seperates them n stuff but that shldnt matter cos thats just a menu... it should be above it right?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

SirChick wrote:above the first script i gave ya.
:?: Now that confuses me even more.
Please try

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
require "include.php";
require "energybarinclude.php";


session_start();

echo '<pre>soldhousesrow: ', print_r($soldhousesrow, true), "</pre>\n";
$_SESSION['Price'] = $soldhousesrow['Price'];
$_SESSION['HouseID'] = $soldhousesrow['SoldHouseID'];
$_SESSION['NumberOfRooms'] = $soldhousesrow['NumberOfRooms'];
$_SESSION['HouseType'] = $soldhousesrow['HouseType'];
echo '<pre>_SESSION', print_r($_SESSION, true), "</pre>\n";
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Woah, this is a long list!

Code: Select all

[b]<br />
<b>Notice</b>:  A session had already been started - ignoring session_start() in <b>C:\xampp\htdocs\include.php</b> on line <b>17</b><br />
<br />
<b>Notice</b>:  A session had already been started - ignoring session_start() in <b>C:\xampp\htdocs\include.php</b> on line <b>17</b><br />
<br />

<b>Notice</b>:  A session had already been started - ignoring session_start() in <b>C:\xampp\htdocs\homeloginvariables.php</b> on line <b>6</b><br />
<br />
<b>Notice</b>:  Undefined index:  HouseType in <b>C:\xampp\htdocs\homeloginvariables.php</b> on line <b>37</b><br />
<br />
<b>Notice</b>:  Undefined index:  HouseStatus in <b>C:\xampp\htdocs\homeloginvariables.php</b> on line <b>38</b><br />

<br />
<b>Notice</b>:  Undefined index:  HouseNumber in <b>C:\xampp\htdocs\homeloginvariables.php</b> on line <b>39</b><br />
<br />
<b>Notice</b>:  Undefined index:  StreetName in <b>C:\xampp\htdocs\homeloginvariables.php</b> on line <b>40</b><br />
<br />

<b>Notice</b>:  Undefined index:  Area in <b>C:\xampp\htdocs\homeloginvariables.php</b> on line <b>41</b><br />
<br />[/b]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

SirChick wrote:Woah, this is a long list!
And still there's something missing: the output of the two print_r statements.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

I think i found it man i must be the worst coder ever theres tons of errors !:

Code: Select all

<b>Notice</b>:  Undefined variable: soldhousesrow in <b>C:\xampp\htdocs\housepurchaselist.php</b> on line <b>10</b><br />
</pre>
<br />
<b>Notice</b>:  Undefined variable: soldhousesrow in <b>C:\xampp\htdocs\housepurchaselist.php</b> on line <b>11</b><br />
<br />

<b>Notice</b>:  Undefined variable: soldhousesrow in <b>C:\xampp\htdocs\housepurchaselist.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>:  Undefined variable: soldhousesrow in <b>C:\xampp\htdocs\housepurchaselist.php</b> on line <b>13</b><br />
<br />
<b>Notice</b>:  Undefined variable: soldhousesrow in <b>C:\xampp\htdocs\housepurchaselist.php</b> on line <b>14</b><br />

<pre>_SESSIONArray
(
    [Current_User] => 1
    [Area] => North East
    [Streetname] => Regent Street
    [HouseType] => 
    [CityID] => 1
    [HouseNumber] => 1
    [Editbox1] => 
    [Price] => 
    [HouseID] => 
    [NumberOfRooms] => 
)
</pre>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

SirChick wrote:Undefined variable: soldhousesrow
There is no $soldhousesrow defined therefore you assign null to all elements of SESSION here
SirChick wrote:$_SESSION['Price'] = $soldhousesrow['Price'];
$_SESSION['HouseID'] = $soldhousesrow['SoldHouseID'];
$_SESSION['NumberOfRooms'] = $soldhousesrow['NumberOfRooms'];
$_SESSION['HouseType'] = $soldhousesrow['HouseType'];
Where's $soldhousesrow supposed to be set?
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

Well this is where its used in the main script on the first post (the top one)

Code: Select all

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value=\"{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}\">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}
But what i dont understand is how HouseType is working... yet Price is not when they both are in the $soldhouserow?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

SirChick wrote:Well this is where its used in the main script on the first post (the top one)

Code: Select all

while($soldhousesrow = mysql_fetch_array($soldhousesresult)) {
        echo "<option value="{$soldhousesrow['Price']}|{$soldhousesrow['HouseType']}">£ {$soldhousesrow['Price']} - {$soldhousesrow['HouseType']}</option>";
}
But you told me that the second snippet
SirChick wrote:<?
include ("include.php");
include ("energybarinclude.php");


session_start();
$_SESSION['Price'] = $soldhousesrow['Price'];
$_SESSION['HouseID'] = $soldhousesrow['SoldHouseID'];
$_SESSION['NumberOfRooms'] = $soldhousesrow['NumberOfRooms'];
$_SESSION['HouseType'] = $soldhousesrow['HouseType'];
?>
is "above" the while loop. So, how can you use $soldhousesrow before it is set? (hint: you can't)
But what i dont understand is how HouseType is working...
No idea. In the output of print_r() it is empty, just as Price is.
SirChick wrote:[Streetname] => Regent Street
[HouseType] =>
[CityID] => 1
[HouseNumber] => 1
[Editbox1] =>
[Price] =>
[HouseID] =>
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

right ok ill put session "below" the while loop and see what happens
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

After the loop $soldhousesrow will be FALSE, not an array.
I have no idea what you're trying to accomplish at that point, therefore I can't give you a more helpful advice, sorry.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

What do you mean by false?

Basically imagine a list box..... displaying 2 fields from a specific table...

which ever one the user has clicked on and then clicked the buy submit button...they should be put into variables...which are also then put into the session to be processed else where.
Post Reply