Jump to content

passing variables via form to query


esiason14

Recommended Posts

I'm trying to pass the player_id as a variable from a drop-down submit form to another page for use in a query.

Here is the code for the form:

[code]$query = mysql_query("SELECT fname, lname, player_id FROM players ORDER by lname ASC");
?>
<form action=test.php method=POST>
<input type="hidden" name="<php echo $player_id ?>" value="<php echo $player_id ?>"> <select name=players>
<?php
while ($r = mysql_fetch_array($query))
{
$fname =  $r["fname"];
$lname = $r["lname"];
$player_id = $r["player_id"];
echo "<option value=$player_id>$fname $lname</option>";
}
echo "</select>";
?>
<input type="submit" name="<php echo $player_id ?>" value="Plot Player" />
<input type="reset">
</form>[/code]

and the partial code for the output page:

[code]$players = $_POST;

if (isset($players))
{

  $SQL = "SELECT * from playerstats ps, players p WHERE ps.player_id='$players' and p.player_id=ps.player_id and ps.year=2005";
  $RESULT = mysql_query($SQL);
  if ($myrow=$RESULT) {
      do {
$labels = $myrow["week"];
        //$data2[] =  (($row['pass_yd'] * 0.034) + ($row['pass_td'] * 6) + ($row['twopt'] * 2) + ($row['int'] * -2) + ($row['fmbl_lost'] * -2) + ($row['rush_yd'] * 0.067) + ($row['rush_td'] * 6));
        $datawk[] = $myrow["week"];
        $data[] = $myrow["pass_yd"];
        $data2[] = $myrow["pass_td"];
$lastname = $myrow["lname"];
$firstname = $myrow["fname"];
        $data_names[] = $myrow["lname"]."(".$myrow["player_id"].")";
      }
while ($myrow=$RESULT);
  }
[/code]

Im trying to pass the variable for use in my graphing application. For some reason I can get it to work..See anything that may help me get it working?
Link to comment
Share on other sites

Hello esiason14,

What I can do is list some code fixes that I think will solve your prob, its little coding mistakes. Im not too sure about the SQL statement on the output page. Anyway here is what I think,

THE POSTING PAGE
[code]
$query = mysql_query("SELECT fname, lname, player_id FROM players ORDER by lname ASC");
?>
<form action="test.php" method="POST"> <!-- Encapsulate = with "" _ codeing standards -->
<?php
while ($r = mysql_fetch_array($query))
{
$fname =  $r["fname"];
$lname = $r["lname"];
$player_id = $r["player_id"];
echo "<option value=\"$player_id\">$fname $lname</option>"; /* encapsulate the value= with "" */
}
echo "</select>";
?>
<input type="submit" name="cmdPlotPlayer" value="Plot Player" /> <!-- dont need to pass player ID for buttons name you have it
from the select box box -->
<input type="reset">
</form>
[/code]

THE OUTPUT PAGE:
[code]
$SQL = "SELECT * from playerstats ps, players p WHERE ps.player_id='$players' and p.player_id=ps.player_id and ps.year=2005";
should be
$SQL = "SELECT * from playerstats ps, players p WHERE ps.player_id='".$players['players']."'". " and p.player_id=ps.player_id and ps.year='2005'";
/* If you do not understand why its $players['players'] then do a print_r($players) on your output page and then you
will, also echo out the SQL statement to make sure it looks correct thats what I do and it always helps on complex queries */
[/code]

Right thats all I can suggest for now let me know how things turn out, I have also included the modified versions of your pages, the ones I ran testing on without the database connections. Cause I dont have the tables

My Form Page :
[b]esiason14_21stJuly2006_form.php[/b]
[code]
<form action="esiason14_21stJuly2006_post.php" method="POST">
<select name="players">
<?php
for ($i=0; $i < 4; $i++){
$fname =  "first".$i;
$lname = "last".$i;
$player_id = "playerId".$i;
echo "<option value=\"$player_id\">$fname $lname</option>";
}
echo "</select>";
?>
<input type="submit" name="<?php echo $player_id ?>" value="Plot Player" />
<input type="reset">
</form>
[/code]

My Output Page:
[b]esiason14_21stJuly2006_post.php[/b]
[code]
<?php
$players = $_POST;

if (isset($players))
{
  $SQL = "SELECT * from playerstats ps, players p WHERE ps.player_id='".$players['players']."'". " and p.player_id=ps.player_id and ps.year='2005'";
  print_r($SQL);
  echo "<br />";
  print_r($players);
}
?>
[/code]

Hope this all makes sense and will bring you to enlightenment  ;)
Cheers
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.