Jump to content

Passing session variable between pages


dpedroia

Recommended Posts

I have a page containing a table that outputs team stats. If you're logged in and the manager of a specific team, a 'Manage' link appears in the table beside that team and may clicked to manage the team on another page.

 

The portion of code that handles the table output on the page that contains the table with the 'Manage' link is:

 

<?php
$max_col = 100;
$query = "SELECT * FROM teams WHERE user = $user";
$result = mysql_query($query) or die(mysql_error());

echo "<table class='sortable tableFormat' cellspacing=5>
<tr>
<th>Team Name   </th>
<th>Sport   </th>
<th>Games   </th>
<th>Wins   </th>
<th>Losses   </th>
<th>Winning %   </th>
<th>Manage</td>
</tr>";

$col = 0;
while($row = mysql_fetch_array($result))
{
        extract($row);
        $col++;

$wpct = @($wins/$games);

session_start();
session_register('team');
$team = $_POST['$id'];

echo "<tr>
<td><a href=standings-team.php?id=$id>$teamname</a>   </td>
<td>$sport   </td>
<td>$games   </td>
<td>$wins   </td>
<td>$losses   </td>
<td>"; echo number_format("$wpct",3); echo "   </td>
<td><a href=manageteam.php?team=$id>Manage</a></td>
</tr>";
}

echo"</table>";
?>

 

The code I have currently for the page to actually manage the team (manageteam.php) is:

 

<?php

session_start();
$id = $_GET[$team];
$max_col = 100;
$query = "SELECT * FROM teams WHERE id=$team";
$result = mysql_query($query) or die(mysql_error());

$wpct = @($wins/$games);
    
echo "Team Name: $teamname<br>
Sport: $sport<br>
Games: $games<br>
Wins: $wins<br>
Losses: $losses<br>
Winning Percentage: number_format($wpct,3)";
?>

 

My goal is to have the user (i.e. "the manager") click the 'Manage' link, which will send that team's unique Team ID to the manageteam.php page so the team information can be displayed. After researching I've seen that session variables can handle this well, but after testing the above code the manageteam.php page is not outputting any of the team information.

 

Any guidance as to where I may be going wrong with my session variable handling would be greatly appreciated.

 

Brian

Link to comment
Share on other sites

Okay,

 

At the very top of all of my .php pages I have:

 

<?php require_once('config.php');
?>

 

My config.php file is:

 

<?php
session_start();
$conn = mysql_connect('HOST','USERNAME','PASSWORD');
mysql_select_db('sportaccess');
?>

 

Because the very first thing on all of the PHP pages is the require_once('config.php');, does this mean I do not have to have a session_start(); elsewhere on my .php pages?

 

Having now taken out all 'session' code related to the team managing feature I'm looking to implement, my code is now as follows...

 

The Page That Outputs All Of a User's Teams:

<?php
$max_col = 100;
$query = "SELECT * FROM teams WHERE user = $user";
$result = mysql_query($query) or die(mysql_error());
    
echo "<table class='sortable tableFormat' cellspacing=5>
<tr>
<th>Team Name   </th>
<th>Sport   </th>
<th>Games   </th>
<th>Wins   </th>
<th>Losses   </th>
<th>Winning %   </th>
<th>Manage</td>
</tr>";

$col = 0;
while($row = mysql_fetch_array($result))
{
        extract($row);
        $col++;
$wpct = @($wins/$games);

echo "<tr>
<td><a href=standings-team.php?id=$id>$teamname</a>   </td>
<td>$sport   </td>
<td>$games   </td>
<td>$wins   </td>
<td>$losses   </td>
<td>"; echo number_format("$wpct",3); echo "   </td>
<td><a href=manageteam.php?team=$id>Manage</a></td>
</tr>";
}

echo"</table>";
?>

 

As you can see in that table, I want the 'Manage' link, when clicked' to redirect to manageteam.php?team=$id with $id being the team's ID.

 

The Page Users Are Redirected To After Clicking Manage (manage.php):

<?php	
$max_col = 100;
$query = "SELECT * FROM teams WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());

$wpct = @($wins/$games);
    
echo "Team Name: $teamname<br>
Sport: $sport<br>
Games: $games<br>
Wins: $wins<br>
Losses: $losses<br>
Winning Percentage: number_format($wpct,3)";
?>

 

I want this page to display the team's information whose ID matches the team's $id from the previous page.

 

I've tried joel24's suggestion and ensured I was opening sessions on both pages (at which point I remembered I had a session_start(); in config.php), but no luck. Is there a session code that should work that stands out to you provided the code above?

 

Thanks again.

Link to comment
Share on other sites

You're passing a GET variable to the manageteam.php script, so there's really no need to use a $_SESSION var at all*. Assuming the team's ID is an integer, place this above the $maxcol = 100; line in manageteam.php. There should be more validation done, but just to see if it works, give it a try.

 

 

EDIT: * referring to the $id variable . . .

 

$id = (int) $_GET['id'];

Link to comment
Share on other sites

You're passing a GET variable to the manageteam.php script, so there's really no need to use a $_SESSION var at all*. Assuming the team's ID is an integer, place this above the $maxcol = 100; line in manageteam.php. There should be more validation done, but just to see if it works, give it a try.

 

 

EDIT: * referring to the $id variable . . .

 

$id = (int) $_GET['id'];

 

Hmm, I see what you're talking about and modified my manageteam.php to the following:

 

<?php

$id = (int) $_GET['id'];
$max_col = 100;
$query = "SELECT * FROM teams WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());

$wpct = @($wins/$games);
    
echo "Team Name: $teamname<br>
Sport: $sport<br>
Games: $games<br>
Wins: $wins<br>
Losses: $losses<br>
Winning Percentage: "; echo number_format("$wpct",3);
?>

 

But there is still no output. If it helps, I made a test account on my website (still under heavy construction, so excuse any misaligned spacing, etc..).

 

[*]Go to http://www.vyfx.com/sportaccess/

[*]Login using the account: test@test.com | test

[*]Visit http://www.vyfx.com/sportaccess/mysa-teams.php (or click My Account > My SportAccess from the top nav menu) and then 'Manage Your Teams'. The 'Manage' link here is the one I am talking about

 

Should I be modifying anything on mysa-teams.php to perhaps assist in the $id = (int) $_GET['id']; line?

 

Thank you!

Link to comment
Share on other sites

I had a typo in that. I had in my head that everything was "id".Make it: $id = (int) $_GET['team'];

I changed my manageteams.php page to:

 

<?php

$id = (int) $_GET['team'];
$max_col = 100;
$query = "SELECT * FROM teams WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());

$wpct = @($wins/$games);
    
echo "Team Name: $teamname<br>
Sport: $sport<br>
Games: $games<br>
Wins: $wins<br>
Losses: $losses<br>
Winning Percentage: "; echo number_format("$wpct",3);
?>

 

But there is still no output. Ahh! There's probably something simple that I've been overlooking. Any other ideas perhaps?

Link to comment
Share on other sites

you're not fetching the array returned from the database,


<?php

$id = (int) $_GET['team'];

$max_col = 100;

$query = "SELECT * FROM teams WHERE id=$id";

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

$teamRow = mysql_fetch_array($result);

//now all your database information is stored in the array $teamRow
// i.e $teamRow['wins']; will be the value stored in a field named "wins"	

$wpct = @($teamRow['wins']/$teamRow['games']);

echo "Team Name: {$teamRow['teamname']}<br>

Sport: {$teamRow['sport']}<br>

Games: $games<br>

Wins: $wins<br>

Losses: $losses<br>

Winning Percentage: "; echo number_format("$wpct",3);
?>

Link to comment
Share on other sites

you're not fetching the array returned from the database,


<?php

$id = (int) $_GET['team'];

$max_col = 100;

$query = "SELECT * FROM teams WHERE id=$id";

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

$teamRow = mysql_fetch_array($result);

//now all your database information is stored in the array $teamRow
// i.e $teamRow['wins']; will be the value stored in a field named "wins"	

$wpct = @($teamRow['wins']/$teamRow['games']);

echo "Team Name: {$teamRow['teamname']}<br>

Sport: {$teamRow['sport']}<br>

Games: $games<br>

Wins: $wins<br>

Losses: $losses<br>

Winning Percentage: "; echo number_format("$wpct",3);
?>

 

I could kiss you right now. Well maybe not, but regardless this worked.. thanks to everyone for the help. Woo!  :D

Link to comment
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.