【问题标题】:How to get data from dropdown list into a table in PHP?如何从下拉列表中获取数据到 PHP 中的表中?
【发布时间】:2017-07-15 12:35:12
【问题描述】:

我有一个已由数据库填充的下拉列表。我正在尝试获取与其表 ID 相关的数据。我可以获取数据,但它返回的是所有数据,而不是特定于某项运动的 ID。当我从下拉列表中选择一项运动时,然后单击我要返回该数据的按钮。

我有两个文件,一个是动作,一个是下拉列表

<form method='POST' action="sportSearch.php">
        <fieldset>
            <div id="dropDownList">
                <select value="sport" name="sport">
                    <?php
                        try {
                            $dropDownSport = $pdo->prepare('SELECT sportName FROM sport');
                            $dropDownSport->execute();
                            $dropDown = $dropDownSport->fetchAll();
                        } catch(PDOException $e) {
                            $error = 'Select statement error';
                            include 'error.html.php';
                            exit();
                        }
                        foreach($dropDown as $row) {
                            echo'<option value='.$row["sportID"].'>'.$row["sportName"].'</option>';
                        }  
                    ?>
                </select>
            </div>
            <div>
                <button type="submit">Submit</button>
            </div>
        </fieldset>
    </form>

下面我试图找到特定运动的 id,但它只是输出

 $sportId = $_POST['sport'];

$where = "";
$search_sport_id = false;

if(is_numeric($sportId)) {
    $search_sport_id = true;
    $where = "WHERE sport.sportID = :sportID";
}

try {
    $selectSport = $pdo->prepare ('SELECT athlete.athleteID, 
    athlete.eventID, athlete.sportID, athlete.lastName, athlete.firstName, 
    athlete.gender, event.eventName, sport.sportName, athlete.gender, 
    athlete.image, athlete.medal
    FROM athlete JOIN event ON event.eventID = athlete.eventID JOIN sport 
    ON sport.sportID = athlete.sportID '.$where);

    if($search_sport_id == true) {
        $selectSport->execute(array(':sportID' => $sportId));
    } else {
       $selectSport->execute(); 
    }

} catch(PDOException $e) {
    $error = 'Select statement error';
    include 'error.html.php';
    exit();
}

然后输出到表格

<table>
    <tr>
        <th>athleteID</th>
        <th>eventID</th>
        <th>sportID</th>
        <th>lastName</th>
        <th>firstName</th>
        <th>eventName</th>
        <th>sportName</th>
        <th>gender</th>
        <th>image</th>
        <th>medal</th>
    </tr>
    <?php
        if(!empty($selectSport)) {
            foreach($selectSport as $row) {
                echo'<tr>';
                    echo'<td>'.$row['athleteID'].'</td>';
                    echo'<td>'.$row['eventID'].'</td>';
                    echo'<td>'.$row['sportID'].'</td>';
                    echo'<td>'.$row['lastName'].'</td>';
                    echo'<td>'.$row['firstName'].'</td>';
                    echo'<td>'.$row['eventName'].'</td>';
                    echo'<td>'.$row['sportName'].'</td>';
                    echo'<td>'.$row['gender'].'</td>';
                    echo'<td><img src="photos/'.$row['image'].'"</td>';
                    echo'<td>'.$row['medal'].'</td>';
                echo'</tr>';
            } 
        } 
    ?>      
</table>

【问题讨论】:

    标签: php list pdo dropdown


    【解决方案1】:

    您的问题是$row['sportID'] 不存在。

    让我们看看您的查询: $dropDownSport = $pdo-&gt;prepare('SELECT sportName FROM sport');

    您只是从表格中选择“运动名称”。你想要的是:

    $dropDownSport = $pdo-&gt;prepare('SELECT sportID, sportName FROM sport');

    我想指出另外两点:

    在 PHP 中调试

    在 PHP 中进行调试非常简单,尤其是在这种情况下。你应该做的是在你的第二个脚本中在$sportId = $_POST['sport']; 之后回显$sportId - 这会告诉你这是一个空变量。或者,您可以在下拉列表中使用浏览器检查功能 - 它会向您显示值是空的。

    面向对象的 PHP

    我对 PHP 最大的厌恶之一是避免使用对象的人。我发现它们在涉及数据库时特别有用。看看 - 它肯定会帮助您解决未来的问题(IMO 面向对象的 PHP 更容易调试)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多