MySQL/PHP 编码程序

MySQL/PHP Coding Program

我有以下代码将我的 phpmyadmin 数据库链接到我的 PHP 脚本。该代码采用 table 的 50 名 NFL 球员及其上赛季的统计数据。现在,我希望能够对其进行编码,以便我可以通过下拉框或某种下拉菜单 box/list select 播放器,然后 selected 和 Table 再次显示,该玩家将不会被列出。但是,我被卡住了,不知道该怎么做。至少有人可以帮助我了解基础知识以及为此我需要什么吗?

<!DOCTYPE html>
<html>
<head>
        <title>PHP Project</title>
        <style>
            table,th,td {
                border: 1px solid navy;
                }
        </style>
</head>

<body>

<?php
    $db_hostname='localhost';
    $db_username='root';
    $db_password='';
    $db_database='Project';

    $connection = new mysqli(   $db_hostname,
                                $db_username,
                                $db_password,
                                $db_database);

    if ($connection->connect_error) {
        echo "Sorry";
    } else {
        echo "Connected!<br><br>";      
        $sql = "SELECT * FROM NFL2014Receiving";
        $result = $connection->query($sql);
        if (!$result) die ($connection->error);
        $n = $result->num_rows;     

        for ($i=1; $i<=$n; $i++) {
            $row = $result->fetch_array(MYSQLI_ASSOC);

        echo "<table>
            <tr><th>ID</th><th>Player</th><th>Team</th>
            <th>Position</th><th>Receptions</th>
            <th>Receiving Yards</th><th>Avg Yds/Catch</th>
            <th>Avg Yds/Game</th><th>Touchdowns</th></tr>";

        echo "<tr><td width=20>" . $row['iD'] . "</td><td width=150>" . $row['Player'] . "</td><td width=40>" .
                $row['Team'] . "</td><td width=30>" . $row['Pos'] . "</td><td width=30>" .
                $row['Rec'] . "</td><td width=40>" . $row['Yds'] . "</td><td width=30>" .
                $row['Avg'] . "</td><td width=40>" . $row['Yds/G'] . "</td><td width=20>" .
                $row['TD'] . "</td></tr>";
        }
        echo "</table>";
    }

?>

</body>
</html>

你可以这样做:

<?php
    $db_hostname='localhost';
    $db_username='root';
    $db_password='';
    $db_database='Project';

    $connection = new mysqli(   $db_hostname,
                                $db_username,
                                $db_password,
                                $db_database);

    if ($connection->connect_error) {
        echo "Sorry";
    } else {
        echo "Connected!<br><br>";      
        $sql = "SELECT * FROM NFL2014Receiving";
        $result = $connection->query($sql);
        if (!$result) die ($connection->error);
        $n = $result->num_rows;     

        $nfl = array();

        echo "<table>
            <tr><th>ID</th><th>Player</th><th>Team</th>
            <th>Position</th><th>Receptions</th>
            <th>Receiving Yards</th><th>Avg Yds/Catch</th>
            <th>Avg Yds/Game</th><th>Touchdowns</th></tr>";

        for ($i=1; $i<=$n; $i++) {
            $row = $result->fetch_array(MYSQLI_ASSOC);
            $nfl[$row['iD']] = $row['Player'];
            if(!isset($_POST['hide']) || $_POST['hide'] != $row['iD']){
                echo "<tr><td width=20>" . $row['iD'] . "</td><td width=150>" . $row['Player'] . "</td><td width=40>" .
                        $row['Team'] . "</td><td width=30>" . $row['Pos'] . "</td><td width=30>" .
                        $row['Rec'] . "</td><td width=40>" . $row['Yds'] . "</td><td width=30>" .
                        $row['Avg'] . "</td><td width=40>" . $row['Yds/G'] . "</td><td width=20>" .
                        $row['TD'] . "</td></tr>";
            }
        }
        echo "</table>";
        echo "<form method='post' action=''><select name='hide'>";
        foreach($nfl as $key=>$value){
            echo "<option value='".$key."'>".$value."</option>";
        }
        echo "<input type='submit' value='Submit'>";
        echo "</select></form>";
    }

?>

试一试,如果对你有用,请告诉我:-)