繁体   English   中英

MySQL PHP根据输入的结果更新另一个表

[英]MySQL PHP update another table based on entered results

使用MySQL和PHP,我创建了一个本地网站,允许我输入尚未输入的灯具的结果,即我可以从列表中选择没有结果的灯具,出现团队名称,即“ Team A vs. Team A vs. B组”,然后输入分数,并将其成功保存到结果表中。

但是,我不确定如何更新团队表统计信息:

例如,如果我进入第3战队击败第4战队4-0 ,则会发生以下情况:

第3队出场+1,赢了+ 1,gf + 2,gd + 2,积分+3

AND小组4出场+ 1,输+1,ga + 2,gd-2

夹具表

| fixture_id | home_team | away_team |

     | 1 |      | 1 |        | 4 |
     | 2 |      | 2 |        | 3 |
     | 3 |      | 4 |        | 3 |

结果表

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 |

如何在PHP和MySQL中对此进行编码?

谢谢。

addResult.php

<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);
}

我认为这是您需要的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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM