I'm working on a directory program that will eventually display various gaming clans to visitors. Right now, I'm working on script that displays the sign-up form. The sign-up form appears on the first page, and if the user did not fill it out correctly, the second page displays the exact same form, highlights the fields that are either blank or incorrect, and displays the information the user typed in or selected on the first page. There's a selection box called "clan_game" in which they need to select what game their clan plays. Each option in the selection box is pulled from a table called "games" that has the columns "game_id" and "game_name" ("game_id" is the value of the option and "game_name" is the name of the option the user sees). Once a user selects a game, then submits the form but has filled something out incorrectly, the selection box needs to have the game they chose on the first page selected on the second.

This is the code I'm using that's apparently not working:

if (isset($clan_game))
	{
	$query_setgame = 'SELECT `game_name` FROM `games` WHERE `game_id` = "$clan_game"';
	$result_setgame = mysql_query($query_setgame) or die ("Query failed.");
	$gamerow = mysql_fetch_array($result_setgame,MYSQL_ASSOC);
	echo "<option value=\"$clan_game\" selected>$gamerow[game_name]</option>\n";
	}

The script knows the game_id but not the game_name, so I want to match the game_id to a game_name in the database, and then put that game's name as the name the user sees for that selected option. But I've run into a "PHP newb" problem, the mySQL query is returning "Resource id #7", and the mysql_fetch_array function returns nothing. Any idea what I can do differently to make this work?

    the problem is in your sql statement. When a statement is in '', any vars inside the '' wont be parsed

    eg

    $name = "adam";
    echo 'my name is $name'; //outputs my name is $name
    echo "my name is $name"; //outputs my name is adam
    

    so you need to change it to

    $query_setgame = "SELECT `game_name` FROM `games` WHERE `game_id` = '$clan_game'";

    see if that works

    adam

      Ah... That simple fix worked perfectly! I've gotta be careful with my quotes...

      Thanks!

        No probs. I always use double quotes " " to surround a statement, and single ' ' inside. It takes slightly longer, because php then parses all your statements, but the difference is negligable.

        Don't forget to mark thread resolved, using the link below. See you on here soon

          Write a Reply...