首页 > 解决方案 > 无法从数据库调用数据 - CPanel 也无法添加或管理对 cPanel 数据库的更改

问题描述

所以我创建了一个连接到数据库的网站,我知道它是连接的,因为它从“用户”数据库获取登录名和密码。网站上还有一个功能,可以根据选择的星期来获得足球比赛的结果,但由于某种原因,结果不显示。如果有人可以查看代码,将不胜感激。我对编码一无所知,因此非常感谢任何帮助!该网站给了我这个错误信息。发生错误:SQLSTATE[HY000] [1045] 用户 ''@'localhost' 的访问被拒绝(使用密码:否)。

CPanel 是基于网络的,与 PHPMyAdmin 相同,它们不是我计算机上的应用程序。

代码:

<?php
$servername = "localhost";
$username = "test";
$password = "test";

function getFixture($fixture_date) {
    try {
        $matches = [];
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->query("SELECT fixture.Match_ID, team_home.name AS home_team_name, team_away.name AS away_team_name, fixture.Date, fixture.Home_team_goals, fixture.Away_team_goals FROM fixture INNER JOIN team AS team_home ON fixture.Home_team = team_home.ID INNER JOIN team AS team_away ON fixture.Away_team = team_away.ID WHERE fixture.Fixture_date = " . $fixture_date);

        while($result = $statement->fetch(PDO::FETCH_ASSOC)){
            array_push($matches, $result);
        }

    }
    catch(PDOException $e) {
        echo "An error occurred: " . $e->getMessage();
    }

    $conn = null;
    return $matches;
}

if (isset($_GET['fixture_date'])){
    $get_fixture_date = $_GET['fixture_date'];
}else{
    $get_fixture_date = 1;
}

$result_matches = getFixture($get_fixture_date);

?>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <meta charset="utf-8">
    <title>Competitions | Oaktown Football Club</title>
</head>
<body>
    <div id="wrapper">
        <header>
            <a href="index.html"><img src="images/logo.png" alt="logo of oaktown football club"/></a>
            <h2>Oaktown Football Club</h2>
        </header>
        <nav>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="competitions.php">Competitions</a></li>
                <li><a href="contact.html">Contact</a></li>
                <li><a href="login.php">Login</a></li>
            </ul>
        </nav>
        <div id="content">
            <p class="content_description">Choose a Fixture Date to be displayed below</p>
            <div id="fixture">
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=1">1</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=2">2</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=3">3</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=4">4</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=5">5</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=6">6</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=7">7</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=8">8</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=9">9</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=10">10</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=11">11</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=12">12</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=13">13</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=14">14</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=15">15</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=16">16</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=17">17</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=18">18</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=19">19</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=20">20</a>
                </div>
                <div class="fixture_date">
                    <a href="competitions.php?fixture_date=21">21</a>
                </div>
            </div>
            <h1>Competitions</h1>
            <p>This week’s game scores are:</p>
            <table id="competitions">
                <thead>
                    <tr>
                        <td>Fixture Date</td>
                        <td>Day</td>
                        <td>Home Team</td>
                        <td>Away Team</td>
                        <td>Home Team Score</td>
                        <td>Away Team Score</td>
                    </tr>
                </thead>
                <tbody>
                <?php for($i=0;$i<count($result_matches);$i++){ ?>
                    <?php $match = $result_matches[$i]; ?>
                    <tr>
                        <td><?php echo $get_fixture_date; ?></td>
                        <td><?php echo $match["Date"]; ?></td>
                        <td><?php echo $match["home_team_name"]; ?></td>
                        <td><?php echo $match["away_team_name"]; ?></td>
                        <td><?php echo $match["Home_team_goals"]; ?></td>
                        <td><?php echo $match["Away_team_goals"]; ?></td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

此外,我无法通过我的管理页面对我的数据库进行修改,我会输入一些内容并检查它是否进入数据库,但它不会在那里。

代码:

session_start();
if (!isset($_SESSION['Name'])) { //does session variable name exist?
    header("location: login.php"); // if not redirect user to login.php web page
}

$servername = "localhost";
$username = "e0892524_admin";
$password = "Administration";

$team_message = "";
$team_id = "";
$team_name = "";
$team_image = "";

$match_message = "";
$match_id = "";
$match_fixture_date = "";
$match_day = "";
$match_home_team = "";
$match_away_team = "";
$match_home_team_score = "";
$match_away_team_score = "";


if (isset($_POST["team_get"])) {
    getTeam($_POST["team_id"]);
}

function getTeam($teamId)
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->query("SELECT * FROM team WHERE ID=" . $teamId);
        $result = $statement->fetch();
        if ($result == null) {
            $GLOBALS['team_message'] = "The ID is not valid or does not exist";
        } else {
            $GLOBALS['team_id'] = $result[0];
            $GLOBALS['team_name'] = $result[1];
        }
    } catch (PDOException $e) {
        echo "An error occurred: " . $e->getMessage();
    }
    $conn = null;
}


if (isset($_POST["team_insert"])) {
    insertTeam();
}

function insertTeam()
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $tmp_image_name = $_FILES["team_image"]["tmp_name"]; //get image file

        if (!isset($tmp_image_name)) { //file hasn't been selected
            $GLOBALS['team_message'] = "Please select a file to upload";
        } else {
            $check = getimagesize($tmp_image_name);  // check if file is an image type

            if ($check) { // if file is an image
                $image_contents = file_get_contents($tmp_image_name);
                $statement = $conn->prepare("INSERT INTO team (ID, Name, Logo) VALUES (:team_id, :team_name, :team_img)");

                $statement->bindValue(":team_id", $_POST["team_id"]);
                $statement->bindValue(":team_name", $_POST["team_name"]);
                $statement->bindValue(":team_img", $image_contents);

                $result = $statement->execute();
                if ($result) {
                    $GLOBALS['team_message'] = "Team record inserted into table successfully";
                } else {
                    $GLOBALS['team_message'] = "The Team record was not inserted";
                }

            } else {
                $GLOBALS['team_message'] = "The file to be uploaded is not an image";
            }
        }

    } catch (PDOException $e) {
        echo "A problem occurred: " . $e->getMessage();
    }

    $conn = null;
}


if (isset($_POST["team_update"])) {
    updateTeam($_POST["team_id"]);
}

function updateTeam($teamId)
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $tmp_image_name = $_FILES["team_image"]["tmp_name"];

        if (!$tmp_image_name) {
            $GLOBALS['team_message'] = 'Please upload a file for this team';
        } else {

            $is_image = getimagesize($tmp_image_name);

            if (!$is_image) {
                $GLOBALS['team_message'] = 'Please upload a valid image for this team';
            } else {
                $image_content = file_get_contents($tmp_image_name);

                $statement = $conn->prepare("UPDATE team SET Name=:team_name, Logo=:team_img WHERE ID=" . $teamId);

                $statement->bindValue(":team_name", $_POST["team_name"]);
                $statement->bindValue(":team_img", $image_content);

                $result = $statement->execute();

                if ($result) {
                    $GLOBALS['team_message'] = "Team record was updated";
                } else {
                    $GLOBALS['team_message'] = "The Team record was not updated";
                }
            }
        }
    } catch (PDOException $e) {
        echo "A problem occurred " . $e->getMessage();
    }

    $conn = null;
}


if (isset($_POST["team_delete"])) {
    deleteTeam($_POST["team_id"]);
}

function deleteTeam($teamId)
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->prepare("DELETE FROM team WHERE ID=" . $teamId);

        $result = $statement->execute();

        if ($result) {
            $GLOBALS['team_message'] = "Team record was deleted successfully";
        } else {
            $GLOBALS['team_message'] = "The Team record was not deleted";
        }
    } catch (PDOException $e) {
        echo "A problem occurred " . $e->getMessage();
    }

    $conn = null;
}


if (isset($_POST["match_get"])) {
    getMatch($_POST["match_id"]);
}

function getMatch($matchId)
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->query("SELECT * FROM fixture WHERE Match_ID=" . $matchId);
        $result = $statement->fetch();
        if ($result == null) {
            $GLOBALS['match_message'] = "The Match ID is not valid or does not exist";
        } else {
            $GLOBALS['match_id'] = $result[0];
            $GLOBALS['match_home_team'] = $result[1];
            $GLOBALS['match_away_team'] = $result[2]; //fix this. how to store in $image = $_FILES["team_image"]["tmp_name"];
            $GLOBALS['match_fixture_date'] = $result[3];
            $GLOBALS['match_day'] = $result[4];
            $GLOBALS['match_home_team_score'] = $result[5];
            $GLOBALS['match_away_team_score'] = $result[6];
        }
    } catch (PDOException $e) {
        echo "An error occurred: " . $e->getMessage();
    }
    $conn = null;
}


if (isset($_POST["match_insert"])) {
    insertMatch();
}

function insertMatch()
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->prepare("INSERT INTO fixture (Match_ID, Home_team, Away_team, Fixture_date, Date, Home_team_goals, Away_team_goals) VALUES (:match_id, :match_home_team, :match_away_team, :match_fixture_date, :match_date, :match_home_team_goals, :match_away_team_goals)");


        $statement->bindValue(":match_id", $_POST["match_id"]);
        $statement->bindValue(":match_home_team", $_POST["match_home_team"]);
        $statement->bindValue(":match_away_team", $_POST["match_away_team"]);
        $statement->bindValue(":match_fixture_date", $_POST["match_fixture_date"]);
        $statement->bindValue(":match_date", $_POST["match_day"]);
        $statement->bindValue(":match_home_team_goals", $_POST["match_home_team_score"]);
        $statement->bindValue(":match_away_team_goals", $_POST["match_away_team_score"]);

        $result = $statement->execute();
        if ($result) {
            $GLOBALS['match_message'] = "Match record inserted into table successfully";
        } else {
            $GLOBALS['match_message'] = "The Match record was not inserted";
        }
    } catch (PDOException $e) {
        echo "A problem occurred: " . $e->getMessage();
    }

    $conn = null;
}


if (isset($_POST["match_update"])) {
    updateMatch($_POST["match_id"]);
}

function updateMatch($matchId)
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->prepare("UPDATE fixture SET Home_team=:match_home_team, Away_team=:match_away_team, Fixture_date=:match_fixture_date, Date=:match_date, Home_team_goals=:match_home_team_goals, Away_team_goals=:match_away_team_goals WHERE Match_ID=" . $matchId);

        $statement->bindValue(":match_home_team", $_POST["match_home_team"]);
        $statement->bindValue(":match_away_team", $_POST["match_away_team"]);
        $statement->bindValue(":match_fixture_date", $_POST["match_fixture_date"]);
        $statement->bindValue(":match_date", $_POST["match_day"]);
        $statement->bindValue(":match_home_team_goals", $_POST["match_home_team_score"]);
        $statement->bindValue(":match_away_team_goals", $_POST["match_away_team_score"]);

        $result = $statement->execute();

        if ($result) {
            $GLOBALS['match_message'] = "Match record was updated";
        } else {
            $GLOBALS['match_message'] = "The Match record was not updated";
        }
    } catch (PDOException $e) {
        echo "A problem occurred " . $e->getMessage();
    }

    $conn = null;
}


if (isset($_POST["match_delete"])) {
    deleteMatch($_POST["match_id"]);
}

function deleteMatch($matchId)
{
    try {
        $conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0892524_OaktownFC", $GLOBALS['e0892524_admin'], $GLOBALS['Administration']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $statement = $conn->prepare("DELETE FROM fixture WHERE Match_ID=" . $matchId);

        $result = $statement->execute();

        if ($result) {
            $GLOBALS['match_message'] = "Match record was deleted successfully";
        } else {
            $GLOBALS['match_message'] = "The Match record was not deleted";
        }
    } catch (PDOException $e) {
        echo "A problem occurred " . $e->getMessage();
    }

    $conn = null;
}

?>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <meta charset="utf-8">
    <title>Admin | Oaktown Football Club</title>
</head>
<body>
<div id="wrapper">
    <header>
        <a href="index.html"><img src="images/logo.png" alt="logo of oaktown football club"/></a>
        <h2>Oaktown Football Club</h2>
    </header>
    <nav>
        <ul>
            <?php //Left to mantain de aesthetic of the web page?>
        </ul>
    </nav>
    <div id="content">
        <h1>Admin</h1>
        <div id="form_1">
            <h3>Team Management</h3>
            <form method="post" enctype="multipart/form-data"
                  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" id="team_form">
                <div class="form_field">
                    <label>ID:</label>
                    <input type="number" min="1" name="team_id" value="<?php echo $team_id; ?>">
                </div>
                <div class="form_field">
                    <label>Team Name:</label>
                    <input type="text" name="team_name" value="<?php echo $team_name; ?>">
                </div>
                <div class="form_field">
                    <label>Team Logo:</label>
                    <input type="file" name="team_image">
                </div>
                <p class="error"><?php echo $team_message; ?></p>
                <input type="submit" name="team_new" value="New" class="button">
                <input type="submit" name="team_insert" value="Insert" class="button">
                <input type="submit" name="team_get" value="Get" class="button">
                <input type="submit" name="team_update" value="Update" class="button">
                <input type="submit" name="team_delete" value="Delete" class="button">
            </form>
        </div>
        <div id="form_2">
            <h3>Fixture Management</h3>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" id="fixture_form">
                <div class="form_field">
                    <label>Match ID:</label>
                    <input type="number" min="1" name="match_id" value="<?php echo $match_id; ?>">
                </div>
                <div class="form_field">
                    <label>Fixture Date:</label>
                    <input type="number" min="1" name="match_fixture_date" value="<?php echo $match_fixture_date; ?>">
                </div>
                <div class="form_field">
                    <label>Day:</label>
                    <input type="date" name="match_day" value="<?php echo $match_day; ?>">
                </div>
                <div class="form_field">
                    <label>Home Team:</label>
                    <input type="text" name="match_home_team" value="<?php echo $match_home_team; ?>">
                </div>
                <div class="form_field">
                    <label>Away Team:</label>
                    <input type="text" name="match_away_team" value="<?php echo $match_away_team; ?>">
                </div>
                <div class="form_field">
                    <label>Home Team Score:</label>
                    <input type="number" min="0" name="match_home_team_score"
                           value="<?php echo $match_home_team_score; ?>">
                </div>
                <div class="form_field">
                    <label>Away Team Score:</label>
                    <input type="number" min="0" name="match_away_team_score"
                           value="<?php echo $match_away_team_score; ?>">
                </div>
                <p class="error"><?php echo $match_message; ?></p>
                <input type="submit" name="match_new" value="New" class="button">
                <input type="submit" name="match_insert" value="Insert" class="button">
                <input type="submit" name="match_get" value="Get" class="button">
                <input type="submit" name="match_update" value="Update" class="button">
                <input type="submit" name="match_delete" value="Delete" class="button">
            </form>
        </div>
    </div>
</div>
</body>
</html>

标签: phphtmlmysql

解决方案


推荐阅读