My code is simply intending to populate two drop down lists with the names of teams and use the users selections to manipulate the points value of those teams. As a trivial example, pick 2 teams and add both their corresponding scores together.
My main problem is trying to get both the users team selection and the corresponding points stored in variables so I can use them. I’m not very familiar with using arrays or how to retreive the value I want.
I can retrieve just the name easily but when I try to retrieve both points and names it populates the ddl incorrectly with “array” in every position. (It’s hosted online here:
My code is below, I’m not sure if the problem is with how I’m creating my drop down list or how I’m retrieving the data.
Thanks for your help
<?
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_user, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$sql="SELECT team, points FROM teams";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$team[$row["team"]]=$row["points"];
$options.="<OPTION VALUE=\"$team\">".$team. '</OPTION>';
}
if (isset($_POST['teamA'])) $teamA = $_POST['teamA'];
else $teamA = "(Not entered)";
if (isset($_POST['teamB'])) $teamB = $_POST['teamB'];
else $teamB = "(Not entered)";
$teamA = htmlspecialchars($teamA);
$teamB = htmlspecialchars($teamB);
?>
<body>
You picked <?php echo $teamA; ?>(with <?php echo $team[$teamA]; ?> points)
and <?php echo $teamB; ?>(with <?php echo $team[$teamB]; ?>)
</br>
<form method="post" action="ddl.php">
Team A:
<SELECT NAME=teamA>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
Team B:
<SELECT NAME=teamB>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT>
<input type="submit" />
</form>
</body>
The problem is how you are building your SELECT list. The correct code would be something like this:
You don’t need to use
$teamvariable.