我目前正在开发一个足球联赛并希望更新我的游戏桌,我给每个游戏一个game_id,并希望使用它作为我的SQL条件更新分数。我的游戏桌有game_id,home_team_id,home_score,away_score,away_team_id,日期,位置....
我的SQL查询:
<?php require_once("includes/functions.php");?>
<?php
if(isset($_POST['update'])){
$errors = array();
//form validation
$required_fields = array("game_id", "home_score", "away_score", );
foreach($required_fields as $fieldname) {
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) &&
$_POST[$fieldname] != 0)) { $errors[] = $fieldname; }
}
if (empty($errors)) {
// Perform Update
$id = mysql_prep($_POST['game_id']);
$home_score = mysql_prep($_POST['home_score']);
$away_score = mysql_prep($_POST['away_score']);
$query = "UPDATE games SET
home_score = {$home_score},
away_score = {$away_score}
WHERE games.game_id= {$id}";
$result = mysql_query($query, $connection);
if (mysql_affected_rows() == 1) {
// Success
$message = "The scorers were successfully updated";
} else {
// Failed
$message = "The scorers update failed ";
$message .= "<br />" . mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form.";
}
} // end: if (isset($_POST['submit']))
?>
<?php include("includes/header.php");?>
<div class="document">
<div class="navigation">
<br />
<div class="content">
<h2>Edit Fixture</h2>
<form action="edit_fixture.php" method="post">
<p>Game Id:
<select type="int" name="game_id" >
<?php
for($count=1; $count <= 70; $count++) {
echo "<option value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<p>Home Score:
<select type="text" name="home_score" >
<?php
for($count=0; $count <= 9; $count++) {
echo "<option value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<p>Away Score:
<select type="text" name="away_score" >
<?php
for($count=0; $count <= 9; $count++) {
echo "<option value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<input type="submit" value="Update" />
</form>
<br />
<a href="content.php">Cancel</a>
</div>
</div>
<?php require("includes/footer.php");?>
答案 0 :(得分:0)
if(isset($_POST['update'])){
您尝试获取的帖子变量与提交按钮不同
<input type="submit" value="Update" />
更新!=更新因此没有发生sql函数,因为未设置post变量
答案 1 :(得分:0)
$_POST['update']
始终为null
,因此您的帖子不会被调用。
将name="update"
添加到提交按钮