Jump to content

[SOLVED] Selecting From An Array Using A Variable


woody86

Recommended Posts

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.

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

 

 

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

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.

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 . '" />';
}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.