I'm a student new to PHP and I have been trying to find a solution for this problem for days. Found all sorts of answers but I don't seem to find something for my exact issue.
I am creating an admin page for a football association as part of my assignment. All is going well until I'm trying to update scores (I re-started them with zero). I get a successful message but the DB is not up updated.
So I tried inserting the score instead, as part of a new fixture, everything else updated but the scores. So I know it's the scores the problem but I don't know why. I made them NOT NULL and then NULL to see if that was the problem, to no avail.
I had initially populated some of the fixtures with scores but after "updating" all of them became either zero or NULL as per the change above.
What am I doing wrong?
if (isset($_POST["Update"])) {
updateScore($_POST["FixtureID"]);
}
function updateScore($FixtureID) {
try {
$conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0912343_Fixtures", GLOBALS['username'], $GLOBALS['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conn->prepare("
UPDATE Fixtures f
, Scores s
SET f.WeekNo = :WeekNo
, f.Fixture_Date = :Fixture_Date
, f.HomeTeam = :HomeTeam
, f.AwayTeam = :AwayTeam
, s.HT_score = :HT_score
, s.AT_score = :AT_score
WHERE f.FixtureID =" . $FixtureID);
$statement->bindValue(":WeekNo", $_POST["WeekNo"]);
$statement->bindValue(":Fixture_Date", $_POST["Fixture_Date"]);
$statement->bindValue(":HomeTeam", $_POST["HomeTeam"]);
$statement->bindValue(":AwayTeam", $_POST["AwayTeam"]);
$statement->bindValue(":HT_score", $_POST["HT_score"]);
$statement->bindValue(":AT_score", $_POST["AT_score"]);
$result = $statement->execute();
if ($result) {
$GLOBALS['message'] = "The score was updated successfully!";
}
else {
$GLOBALS['message'] = "The score was not updated!";
}
}
catch(PDOException $e) {
echo "We are sorry. A problem occured: " . $e->getMessage();
}
$conn = null;
}
```
if (isset($_POST["Insert"])) {insertFixture();
}
function insertFixture() {
try {
$conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=e0912343_Fixtures", $GLOBALS['username'], $GLOBALS['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conn->prepare("
INSERT INTO Fixtures (FixtureID, WeekNo, Fixture_Date, HomeTeam, AwayTeam) VALUES (:FixtureID, :WeekNo, :Fixture_Date, :HomeTeam, :AwayTeam);
INSERT INTO Scores (HT_score, AT_score) VALUES (:HT_score,:AT_score)");
$statement->bindValue(":FixtureID", $_POST["FixtureID"]);
$statement->bindValue(":WeekNo", $_POST["WeekNo"]);
$statement->bindValue(":Fixture_Date", $_POST["Fixture_Date"]);
$statement->bindValue(":HomeTeam", $_POST["HomeTeam"]);
$statement->bindValue(":AwayTeam", $_POST["AwayTeam"]);
$statement->bindValue(":HT_score", $_POST["HT_score"]);
$statement->bindValue(":AT_score", $_POST["AT_score"]);
$result = $statement->execute();
if ($result) {
$GLOBALS['message'] = "The fixture was inserted successfully!";
} else {
$GLOBALS['message'] = "The fixture was not inserted!";
}
}
catch(PDOException $e) {
echo "We are sorry. A problem occurred: " . $e->getMessage();
}
$conn = null;
}
```
```
<header>
<table>
<tr>
<td><h1>Football Association Admin Page</h1></td>
</tr>
</table>
</header>
<section>
<h1>Welcome to the Football Association Admin Page</h1>
<h2>Fixtures Maintenance</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Fixture ID: </td>
<td><input type="text" name="FixtureID" value="<?php echo $fixtureid;?>"></td>
<td><input type="submit" name="Get" value="Get"></td>
</tr>
<tr>
<td> Week No:</td>
<td><input type="text" name="WeekNo" value="<?php echo $weekNo;?>"></td>
<td><input type="submit" name="New" value="New"></td>
</tr>
<tr>
<td>Fixture Date:</td>
<td><input type="text" name="Fixture_Date" value="<?php echo $fixture_date;?>"></td>
<td><input type="submit" name="Update" value="Update"></td>
</tr>
<tr>
<tr>
<td>Home Team:</td>
<td><input type="text" name="HomeTeam" value="<?php echo $homeTeam;?>"></td>
</tr>
<tr>
<td>Home Team Score:</td>
<td><input type="text" name="Home_Team_Score" value="<?php echo $htscore;?>"></td>
<td><input type="submit" name="Insert" value ="Insert"></td>
</tr>
<tr>
<td>Away Team:</td>
<td><input type="text" name="AwayTeam" value="<?php echo $awayTeam;?>"></td>
</tr>
<tr>
<td>Away Team Score:</td>
<td><input type="text" name="Away_Team_Score" value="<?php echo $atscore;?>"></td>
</tr>
<tr>
<td colspan=3><?php echo $message; ?></td>
</tr>
</table>
</form>
</section>
</body>
</html>
```
In your form you use Home_Team_Score
and Away_Team_Score
, but after posting these become $_POST["HT_score"]
and $_POST["AT_score"]
.
These are clearly not the same, and therefore it cannot work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.