Re: session trouble



AccentWebDesign@xxxxxxxxx wrote:
On Feb 8, 11:56 am, "AccentWebDes...@xxxxxxxxx"
<AccentWebDes...@xxxxxxxxx> wrote:
On Feb 8, 5:30 am, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:



AccentWebDes...@xxxxxxxxx wrote:
On Feb 7, 3:16 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
AccentWebDes...@xxxxxxxxx wrote:
On Feb 7, 1:37 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
AccentWebDes...@xxxxxxxxx wrote:
I tried searching but still couldn't figure out my problem. I am
making an admin section, the first page lists the rows in the table in
a drop down menu. The selected item selects which row in the table is
to be diplayed on page 2, it is supposed to pass the info on to the
next page but is not. I must be using sessions improperly but do not
know how to fix it. I am using: session_start();
session_register("team_name"); at the top of the first page and
session_start(); session_register("team_name"); on top of second.
Please help. Here is the complete code:
Joe
First of all, please learn to format your code properly. It will help
you a lot in the long run:
<?php
session_start();
$con =
mysql_connect("p41mysql61.secureserver.net","volleyhigh","Laurarh1");
if (!$con) {
die('Could not connect: ' . mysql_error());}
mysql_select_db("volleyhigh", $con);
$sql = "SELECT team_name, school_club FROM 2_24_12 ORDER BY
team_name";
$result = @mysql_query($sql, $con) or die("couldn't execute query");
while ($row = mysql_fetch_array($result)) {
$team_name = $row['team_name'];
$school_club = $row['school_club'];
$option_block .= "<option value=\"$team_name\">$team_name,
$school_club</option>";}
$_SESSION['$team_name'] = "team_name";
$display_block =
"<form method=\"post\" action=\"../show_mod/mod_2_24_12.php\">
<p><strong>Team:</strong>
<select name=\"team_name\">$option_block</select>
<input type=\"submit\" name\"submit\" value=\"select this team\">
</p>
</form>";
?>
I had to indent continuation lines because of the line length limits.
Note that you're building your display block AFTER your loop. So you
will only get one item in the block. Also, you are setting the
$_SESSION variable incorrectly - it should be
$_SESSION['team_name'] = $team_name;
You need to build your display in the loop, or rather than build a
string, just echo the values directly.
And you need to get the selected team from the $_POST value - that's
what contains information from the client. $_SESSION values are only on
your server and the user has no interaction at all with them.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
Thanks for the help, I moved the display block into the loop and am
getting options in the drop down menu, but still do not seem to be
passing/catching the 'team_name' to the next page. Here is the code:
form 1:
<?php session_start();
$con =
mysql_connect("p41mysql61.secureserver.net","volleyhigh","Laurarh1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("volleyhigh", $con);
$sql = "SELECT team_name, school_club FROM 2_24_12 ORDER BY
team_name";
$result = @mysql_query($sql, $con) or die("couldn't execute query");
while ($row = mysql_fetch_array($result))
{
$team_name = $row['team_name'];
$school_club = $row['school_club'];
$option_block .= "<option value=\"$team_name\">$team_name,
$school_club</option>";
$display_block = "
<form method=\"post\" action=\"../show_mod/mod_2_24_12.php\">
<p><strong>Team:</strong>
<select name=\"team_name\">$option_block</select>
<input type=\"submit\" name\"submit\" value=\"select this team\">
</p>
</form>
";
}
?>
form 2:
<?php session_start();
$con =
mysql_connect("p41mysql61.secureserver.net","volleyhigh","Laurarh1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("volleyhigh", $con);
$_POST["team_name"] = $team_name;
$sql = "SELECT team_name, school_club, contact, coach, phone,
alt_phone, email, address, state, zip, status, win, loss
FROM 2_24_12 WHERE team_name = team_name
";
$result = @mysql_query($sql, $con) or die("couldn't execute query");
$team_name=mysql_result($result,"team_name");
$school_club=mysql_result($result,"school_club");
$contact=mysql_result($result,"contact");
$coach=mysql_result($result,"coach");
$phone=mysql_result($result,"phone");
$alt_phone=mysql_result($result,"alt_phone");
$email=mysql_result($result,"email");
$address=mysql_result($result,"address");
$state=mysql_result($result,"state");
$zip=mysql_result($result,"zip");
$status=mysql_result($result,"status");
$win=mysql_result($result,"win");
$loss=mysql_result($result,"loss");
?>
then I have a form where each value should be displayed so it can be
changed, as in:
<input type="text" name="school_club" value="<? echo "$school_club"; ?
" size="35" maxlength="35" />
I have tried different combinations in the following lines using the
$_POST to get the team_name previously selected to identify the
correct entry to call but can't seem to get anything to work.
$_POST["team_name"] = $team_name;
$sql = "SELECT team_name, school_club, contact, coach, phone,
alt_phone, email, address, state, zip, status, win, loss
FROM 2_24_12 WHERE team_name = team_name
";
As it is know the second form is displaying the first team name in
every block...please help
Joe
Joe,
Don't set $_POST['team_name']. You need to get the value from it.
These are all pretty basic questions - ones covered in the first few
hours of our PHP course. Might I suggest you invest in a good PHP book.
It will be a lot less frustrating to you to learn that way rather than
the way you're going. And you'll learn some good programming techniques
at the same time.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
I think thats a great idea, I will go to the bookstore in the
afternoon...in the mean time could you answer for me, how do I get the
$_POST value so that the program can identify the mysql row to select
in order to display the correct info associated with the 'table
name?' If its not too much trouble..this is driving me batty..

I understand how to use $_POST to pass pass information from one form
the second, my confusion is when the rows are pulled from the table
and put into a drop down menu, identifying on the next page which row
has been selected.


OK, first of all, if you're going to use the same input field name for each school club, you need to make it an array, i.e.

<input type="text" name="school_club[]"
value="<? echo "$school_club"; ?>" size="35" maxlength="35" />

Then the $_POST variable will contain an array of school clubs, i.e.

$_POST['school_club'][0]
$_POST['school_club'][1]
$_POST['school_club'][2]

etc.

The problem here is going to be since you have multiple school clubs, it's going to be hard to relate a particular club to the entry in your database if it going to be changed. You should *never* depend on the order returned by MySQL to match the two (what happens if, for instance, someone added or deleted a club in the middle?).

There are a couple of ways around this. One is to have a separate form for each club, each with it's own submit button. There you wouldn't need to use the arrays noted above - but the user can only update one at a time. Alternatively, have a list of all the teams on one page and let the user select which he wants to change.

Another way is to add a suffix to each field, often times the primary key for the table. Then you would have a hidden field with the suffix value, i.e.

assuming $key is the value of the primary key for the current row:

<input type="text" name="school_club_<?php echo $key;?>"
value="<? echo "$school_club"; ?>" size="35" maxlength="35" />
<input type="hidden" name="keys[]" value=<?php echo $key;?>/>

Now your input fields will be in (assuming $key=1, 3, 5, etc.)

$_POST['school_club_1']
$_POST['school_club_3']
$_POST['school_club_5']

And you keys will be in as:

$_POST['keys'][0] = 1
$_POST['keys'][1] = 3
$_POST['keys'][2] = 5

Does this help?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================

.