php - MySQL/PHP编码程序

标签 php mysql database phpmyadmin

我有以下代码将我的 phpmyadmin 数据库链接到我的 PHP 脚本。该代码采用了一张包含 50 名 NFL 球员及其上赛季统计数据的表格。现在,我希望能够对其进行编码,以便我可以通过下拉框或类似的某种下拉框/列表选择一个播放器,然后在选择并再次显示表格时,不会列出该播放器。但是,我被卡住了,不知道该怎么做。至少有人可以帮助我了解基础知识以及为此我需要什么吗?

<!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>";
    }

?>

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

关于php - MySQL/PHP编码程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736595/

相关文章:

python - 如何将 Django 开发者服务器连接到 MySQL 数据库?

返回SQL结果的php函数

php - 使用 PHP 使用 .sql 文件构建数据库

mysql - 仅选择第一行 - 与 MySQL/SQL Server 兼容?

mysql - FATAL : role "root" does not exist, Rails 仅在网站上,Rails 控制台有效

json - Flutter 如何在本地保存列表数据

php - Zend_Validate_EmailAddress 与 filter_var(..., FILTER_VALIDATE_EMAIL)

php - PHP 中内置函数 setcookie() 和 setrawcookie() 的实际实际差异是什么?

php - 根据现有字段更新列

php - PHP中的通知系统