如何在PHP中从下拉列表中获取数据到表中?

如何在PHP中从下拉列表中获取数据到表中?,php,list,pdo,dropdown,Php,List,Pdo,Dropdown,我有一个由数据库填充的droplist。我正在尝试获取与其表ID相关的数据。我可以获取数据,但它返回的是所有数据,而不是特定于某项运动的ID。当我从下拉列表中选择一项运动时,然后单击我想要返回该数据的按钮 我有两个文件,一个是动作,另一个是下拉列表 <form method='POST' action="sportSearch.php"> <fieldset> <div id="dropDownList">

我有一个由数据库填充的droplist。我正在尝试获取与其表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>
然后输出到表中

<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>

运动员
事件
运动型
姓氏
名字
事件名
运动名
性别
形象
奖章

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

让我们看看您的查询:
$dropDownSport=$pdo->prepare('selectsportname fromsport')

您只能从表中选择“sportName”。你想要的是:

$dropDownSport=$pdo->prepare('SELECT sportID,sportName FROM sport')

我想指出另外两件事:

在PHP中调试

PHP中的调试可以相当直接,尤其是在这种情况下。您应该做的是在
$sportId=$\u POST['sport']之后回显
$sportId
在第二个脚本中-这将向您显示这是一个空变量。或者你可以在你的下拉列表中使用浏览器检查功能——它会告诉你这些值是空的

面向对象的PHP


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

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

让我们看看您的查询:
$dropDownSport=$pdo->prepare('selectsportname fromsport')

您只能从表中选择“sportName”。你想要的是:

$dropDownSport=$pdo->prepare('SELECT sportID,sportName FROM sport')

我想指出另外两件事:

在PHP中调试

PHP中的调试可以相当直接,尤其是在这种情况下。您应该做的是在
$sportId=$\u POST['sport']之后回显
$sportId
在第二个脚本中-这将向您显示这是一个空变量。或者你可以在你的下拉列表中使用浏览器检查功能——它会告诉你这些值是空的

面向对象的PHP

我在PHP中最讨厌的人之一就是那些避免使用对象的人。我发现它们在数据库方面特别有用。看一看——它肯定会帮助您解决未来的问题(IMO面向对象的PHP更容易调试)

<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>