woody86 Posted September 28, 2007 Share Posted September 28, 2007 I've searched all over (including these forums) and can't find an answer, or an answer I can understand, to my problem. I'm fairly new to php and I'm trying to setup a simple database to keep track of hockey pool stats etc. If I send a url of "update.php?Game=Game06", what I want to be able to do is echo $Points to display the data held in my database under the field 'Game06'. I'm hoping someone can figure out what I'm talking about and help me out on this! What follows is what I attempted to do, obviously with no luck $Team = $_GET['Team']; $Game = $_GET['Game']; $Points = $myrow[$Game]; $result = mysql_query("SELECT * FROM data WHERE Team='$Team'",$db); $myrow = mysql_fetch_array($result); Thanks for any insight, Woody. Link to comment https://forums.phpfreaks.com/topic/71019-solved-selecting-from-an-array-using-a-variable/ Share on other sites More sharing options...
pezzie123 Posted September 28, 2007 Share Posted September 28, 2007 Hi, $Team = $_GET['Team']; $Game = $_GET['Game']; $Points = $myrow[$Game]; $result = mysql_query("SELECT * FROM data WHERE Team='$Team'",$db); $myrow = mysql_fetch_array($result); what you need first is mysql_real_escape_string() around all your $_GET as it will prevent sql injection (hacking into your database). so : $Team = mysql_real_escape_string($_GET['Team']); $Game = mysql_real_escape_string($_GET['Game']); $Points = mysql_real_escape_string($myrow[$Game]); then you want to be looping through the array by using while $result = mysql_query("SELECT * FROM data WHERE Team='$Team'",$db); while($myrow = mysql_fetch_array($result)){ $var1 = $myrow['column_name1']; $var2 = $myrow['column_name2']; $var3 = $myrow['column_name3']; echo $var1; echo $var2; and so on ............ } mysql_free_result($result); i hope that helped Link to comment https://forums.phpfreaks.com/topic/71019-solved-selecting-from-an-array-using-a-variable/#findComment-357089 Share on other sites More sharing options...
Dragen Posted September 28, 2007 Share Posted September 28, 2007 well what you need is to search the database for the game field, not the team then. something like: $Team = $_GET['Team']; $Game = $_GET['Game']; $Points = $myrow[$Game]; $result = mysql_query("SELECT * FROM `data` WHERE `Game`='$Game'",$db) or die(mysql_eror()); while($myrow = mysql_fetch_array($result)){ echo $myrow['Game']; }else{ die(mysql_error); } Link to comment https://forums.phpfreaks.com/topic/71019-solved-selecting-from-an-array-using-a-variable/#findComment-357091 Share on other sites More sharing options...
woody86 Posted September 28, 2007 Author Share Posted September 28, 2007 The reason I can't search by game is because of the way I have my database set up. For each entry I have: PlayerID, PlayerName, Team, Owner, Game01, Game02, Game03 ... Game81, Game82. (Most likely a better way to do that too, but thats for another day) If I were to send "update.php?Team=Vancouver&Game=Game02", what I want to see is a list of Vancouver players and their respective points in Game02. Right now I can list the players no problem, I just can't get that value from Game02. $Team = $_GET['Team']; $Game = $_GET['Game']; $Points = $myrow[$Game]; $result = mysql_query("SELECT * FROM data WHERE Team='$Team'",$db); $myrow = mysql_fetch_array($result); ?> <form method="post" action="<? echo $PHP_SELF ?>"> <input type=hidden name="PlayerID" value="<? echo $myrow["PlayerID"] ?>"> <? echo $myrow["Team"] ?> - <? echo $myrow["PlayerName"] ?> - <? echo $myrow["Owner"] ?> Points:<input type="Text" name="<? echo $Game ?>" value="<? echo $Points ?>"><br> <input type="Submit" name="submit" value="Enter information"> </form> Aside from the general coding and security mistakes I've made, I'm concerned that "$Points = $myrow[$Game];" just isn't valid... If i want $Points = $myrow['Game02'];, and $Game = 'Game02', shouldn't I get my result? As it stands I get no output from $Points when I try to echo it. Link to comment https://forums.phpfreaks.com/topic/71019-solved-selecting-from-an-array-using-a-variable/#findComment-357100 Share on other sites More sharing options...
Dragen Posted September 28, 2007 Share Posted September 28, 2007 is it this bit which sets what game you're searching for? Points:<input type="Text" name="<? echo $Game ?>" value="<? echo $Points ?>"> If so $Game = $_GET['Game']; wont call up that variable, unless $Game = 'Game'. I'd have a separate input for the game. perhaps even radios. Then you can have: for($i = 1; $i <= 6; $i++){ echo '<input type="radio" name="Game" value="Game0' . $i . '" />'; } Link to comment https://forums.phpfreaks.com/topic/71019-solved-selecting-from-an-array-using-a-variable/#findComment-357109 Share on other sites More sharing options...
woody86 Posted September 28, 2007 Author Share Posted September 28, 2007 Got it sorted out! $Points = $myrow['$Game'] - Didn't work $Points = $myrow["$Game"] - Did.. Screwed by the single quotes.. and probably not for the last time either. Thank you all for your help!!! I knew it would have to end up being something ridiculous... Also thanks for the security tips, I didn't realize I would be left that open to injection! Woody. Link to comment https://forums.phpfreaks.com/topic/71019-solved-selecting-from-an-array-using-a-variable/#findComment-357116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.