无法存储下拉值,并在查询中使用

时间:2013-12-14 15:14:03

标签: php mysql

您好,我在提交时存储值,然后在下一个查询中使用它时遇到问题。下拉列表填充正常,但它不是存储值或我在查询中动态使用它的方式是错误的。任何帮助表示赞赏。我知道我可能遗漏了一些明显或小的东西,我是php的新手。

下拉框

    $sth = $db->prepare("SELECT DISTINCT WEEK FROM Player_Points_2013");
  //$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$sth->execute();

echo '<form method="POST" action=" " >';
echo '<select id="week" name="week"><OPTION>';
echo "Select a Week to see the stats</OPTION>";

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) 
{
echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>";
}
echo '</SELECT>';
echo '<input type="submit" name="table_stats" value="Submit"/>';
echo '</form>';

$select_val = $_POST['week'];

//使用下拉值来显示周数据

if(isset($_POST['table_stats'])){
  $sql = $db->prepare("SELECT PName,CombinedPoints FROM `Player_Points_2013`  WHERE

    WEEK = '$select_val' ORDER BY CombinedPoints ");
    //$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $sql->execute();
    Print "<table border cellpadding=10 width=500>"; 
    Print "<tr><th>Player</th><th>Points</th></tr>"; 
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
 { 

 Print " <td>".$row['PName'] . "</td> ";
 Print " <td>".$row['CombinedPoints'] . "</td> </tr>";

 } 
 Print "</table>"; 
}
else {
echo $select_val;
}


?> 

1 个答案:

答案 0 :(得分:1)

使用$row$info但不能同时使用,例如:

while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{ 

    Print " <td>".$row['PName'] . "</td> ";
    Print " <td>".$row['CombinedPoints'] . "</td> </tr>";

}

你也有代码:

echo "<option value=\"$WEEK\">" . $row['WEEK'] . "</option>"; 

什么是$WEEK?也许它应该是$row['WEEK']而不是?