Jump to content


Photo

passing variables via form to query


  • Please log in to reply
2 replies to this topic

#1 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 21 July 2006 - 03:59 AM

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:

$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>

and the partial code for the output page:

$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);
   }

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?


#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 21 July 2006 - 05:01 AM

$players = $_POST['players'];
Life's too short for arguing.

#3 manichean

manichean
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 21 July 2006 - 05:18 AM

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
$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>

THE OUTPUT PAGE:
$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 */

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 :
esiason14_21stJuly2006_form.php
<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>

My Output Page:
esiason14_21stJuly2006_post.php
<?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);
}
?>

Hope this all makes sense and will bring you to enlightenment  ;)
Cheers




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users