I'm currently developing a soccer league and would like to update my games table, I have given each game a game_id and want to update the scores using it as a condition in my SQL. my games table has game_id, home_team_id, home_score,away_score, away_team_id, date, location....
My SQL query:
<?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");?>
mysql_
functions are soon to be deprecated. – Polynomial Mar 9 '14 at 12:44