PHP HTML select box selected from MYSQL -
a simple code inserts list of teams in select box. set selected team id , in href
http://localhost/teams.php?id=7&years=2011&cups=8 <?php $query = "select distinct t.team_id,t.team teams t,years y,cups c t.team_id=c.team_id , y.year_id=$_get[years] , c.cup_id=$_get[cups] order t.team asc"; $res = mysql_query($query); $option = ''; while($row = mysql_fetch_assoc($res)) { $option .= '<option value = "'.$row['team_id'].'">'.$row['team'].'</option>'; } ?> <form> <select id="tteam" name="team"> <?php echo $option; ?> </select> </form>
the problem set team_id=$_get[id], shows 1 team. want team=7 selected, others still showing in select box
please aware you're vulnerable sql injections. see: how can prevent sql injection in php?
with said, need use conditional statement compares $row["team_id"]
$_get["id"]
.
while($row = mysql_fetch_assoc($res)) { if($row["team_id"] == $_get["id"]) $option .= '<option value = "'.$row['team_id'].'" selected="selected">'.$row['team'].'</option>'; else $option .= '<option value = "'.$row['team_id'].'">'.$row['team'].'</option>'; }
Comments
Post a Comment