使用MySQL和PHP我创建了一个本地网站,允许我输入尚未输入的灯具的结果,即我可以选择没有列表结果的灯具,团队名称出现即" A队与B队"然后我输入分数并成功保存到结果表。
但是,我不确定如何更新团队表统计信息:
e.g。如果我进入Team 3击败Team 4 2-0 ,则应该发生以下情况:
第3队打+ 1,赢得+1,gf + 2,gd + 2,积分+3
和 第4队出场+1,失去+1,加+ 2,gd-2
灯具表
Result_ID | Fixture_ID | Home_Goals | Away_Goals |
| 1 | | 1 | | 2 | | 0 | i.e home team wins 2-0.
| 2 | | 2 | | 2 | | 2 |
结果表
team_id | team_name | played | won | drawn | lost | gf | ga | gd | points |
| 3 | Team 3 | 37 | 30 | 6 | 2 |95 |22 |73| 96 |
| 4 | Team 4 | 37 | 27 | 7 | 4 |104|40 |64| 88 |
团队表
<form method="POST" id="selectFixture">
<table>
<tr>
<?php
echo '<td> <select name ="fixture_id">';
$stmt = $pdo->prepare('SELECT f.*, t1.Team_Name AS Home, t2.Team_Name AS Away
FROM Fixture f
INNER JOIN Team t1 ON f.Home_Team = t1.Team_ID
INNER JOIN Team t2 ON f.Away_Team = t2.Team_ID
LEFT JOIN Result r ON f.Fixture_ID = r.Fixture_ID
WHERE r.Result_ID IS NULL');
$stmt->execute();
foreach ($stmt as $row) {
echo '<option value="' . $row['Fixture_ID'] . '">' . $row['Home'] . ' v ' . $row['Away'] . '</option>';
}
?>
</select>
</tr>
<tr>
<td>Home Team Goals: </td>
<td><input type="text" name="homeG"></td>
</tr>
<tr>
<td>Away Team Goals: </td>
<td><input type="text" name="awayG"></td>
</tr>
<tr>
<td> <button type="submit" value="Submit" name="submit"/>Submit</button>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])) {
$result = [
'Fixture_ID' => $_POST['fixture_id'],
'Home_Goals' => $_POST['homeG'],
'Away_Goals' => $_POST['awayG'],
];
insert($pdo, 'Result', $result);
echo 'Result added.';
header("Refresh:0");
}
function insert($pdo, $table, $record)
{
$keys = array_keys($record);
$values = implode(', ', $keys);
$valuesWithColon = implode(', :', $keys);
$query = 'INSERT INTO ' . $table . ' (' . $values . ') VALUES (:' . $valuesWithColon . ')';
$stmt = $pdo->prepare($query);
$stmt->execute($record);
}
如何在PHP&amp;中编写代码? MySQL的?
谢谢。
addResult.php
select * from Data where cpty_type = 'INTERBRANCH' and (settlementDate >= '2017-04-18 00:00:00.000' or settlementDate = '1899-12-30 00:00:00.000'))
答案 0 :(得分:2)
我认为这是您需要的UPDATE
查询。
if(isset($_POST['submit'])) {
$result = [
'Fixture_ID' => $_POST['fixture_id'],
'Home_Goals' => $_POST['homeG'],
'Away_Goals' => $_POST['awayG'],
];
insert($pdo, 'Result', $result);
$stmt = $pdo->prepare('
UPDATE Fixture AS f
JOIN Team AS t1 ON f.Home_Team = t1.Team_ID
JOIN Team AS t2 ON f.Away_Team = t2.Team_ID
SET t1.played = t1.played + 1,
t1.won = IF(:Home_Goals > :Away_Goals, t1.won + 1, t1.won),
t1.drawn = IF(:Home_Goals = :Away_Goals, t1.drawn + 1, t1.drawn),
t1.lost = IF(:Home_Goals < :Away_Goals, t1.lost + 1, t1.lost),
t1.gf = t1.gf + :Home_Goals,
t1.ga = t1.ga + :Away_Goals,
t1.gd = t1.gd + :Home_Goals - :Away_Goals,
t1.points = t1.points +
CASE WHEN :Home_Goals > :Away_Goals THEN 3
WHEN :Home_Goals = :Away_Goals THEN 1
ELSE 0
END,
t2.played = t2.played + 1,
t2.won = IF(:Away_Goals > :Home_Goals, t2.won + 1, t2.won),
t2.drawn = IF(:Away_Goals = :Home_Goals, t2.drawn + 1, t2.drawn),
t2.lost = IF(:Away_Goals < :Home_Goals, t2.lost + 1, t2.lost),
t2.gf = t2.gf + :Away_Goals,
t2.ga = t2.ga + :Home_Goals,
t2.gd = t2.gd + :Away_Goals - :Home_Goals,
t2.points = t2.points +
CASE WHEN :Away_Goals > :Home_Goals THEN 3
WHEN :Away_Goals = :Home_Goals THEN 1
ELSE 0
END
WHERE f.Fixture_ID = :Fixture_id');
$stmt->execute($result);
echo 'Result added.';
header("Refresh:0");
}