我在将表单数据保存到数据库时遇到问题

时间:2019-07-19 21:16:07

标签: php mysql

我的项目涉及比赛联赛。有5个玩家,每个玩家必须打另外4个玩家两次,总共进行20场比赛。一张桌子有5列,其中包括ID(未显示),因此每个人都可以看到其他人必须玩的东西和玩过的东西。这是灯具列表。另一个是排行榜,每位球员都排成一列,并显示出打,赢,平局,输,分,对分和分差。你可以在这里看到它: https://the-emg.eu/words/index.php。每个玩家必须输入每个分数 “主场”比赛。我已经用分数输入表格设置了固定装置,这很好用。但是我无法让联盟榜上班。我敢肯定我很想念一些简单的事情,但我只是想不通。

该项目托管在TSO Host提供的我自己的服务器上。

这是我到目前为止的代码:

$servername = "x.x.x.x";
$username = "my name";
$password = "abcdef";
$dbname = "my db";
// Create connection
$conn = new mysqli( $servername, $username, $password, $dbname );
// Check connection
if ( $conn->connect_error ) {
    die( "Connection failed: " . $conn->connect_error );
}
// Get data from form
$hp = $_POST[ "homep" ];
$GLOBALS['hp'] = $hp;
$hs = $_POST[ "homescore" ];
$ap = $_POST[ "awayp" ];
$GLOBALS['ap'] = $ap;
$as = $_POST[ "awayscore" ];
$h_wdl = $hs - $as;
$a_wdl = $hs - $as;
if ($h_wdl = 0){
    $draw = 1; 
}
if ($h_wdl > 0){
    $h_win = 1; 
}
if ($h_wdl < 0){
    $a_win = 1;
}
// Check if this match has already been played
// 
$sql1 = "SELECT * from word_game
                WHERE home_player = '$hp'
                AND away_player = '$ap'";
if ( $result = mysqli_query( $conn, $sql1 ) ) {
    while ( $row = $result->fetch_assoc() ) {
        if ( mysqli_num_rows( $result ) > 0 )
        $home_player = ( $row[ 'home_player' ] );
        $away_player = ( $row[ 'away_player' ] );
        $home_score = ( $row[ 'home_score' ] );
        $away_score = ( $row[ 'away_score' ] );
        {
            if ( $home_score > 1 ) { //check if home score is already recorded
                goto a;
            } // Exit without saving as match already played
            else {
                goto b;
            } // Continue to record score for this match
        }
    }
}
b:
// Update home player record ==============================================
$sql2 = "UPDATE word_game 
            SET home_score = '$hs',
            away_score = '$as'
            WHERE home_player = '$hp' 
            AND away_player = '$ap'";
mysqli_query( $conn, $sql2 );

//=========================================== ALL GOOD ABOVE 

// sql3 Select existing from word_game after update  HOME PLAYER ==========

$sql3 = "SELECT SUM(home_score) from word_game
                WHERE home_player = '$hp'";
if ( $result = mysqli_query( $conn, $sql3 ) ) {
    while ( $row = $result->fetch_assoc() ) {
        $new_home_score = $row['SUM(home_score)'];
        echo $hp;
        echo $new_home_score;
        echo $GLOBALS ['hp'];
        echo $new_home_score;       
}}
$sql2
The code above in $sql3 selects the variables and echoes correctly
The update $sql4 below does ABSOLUTELY NOTHING!!

$sql4 = "UPDATE word_players 
            SET points_for = $new_home_score
            WHERE player = $GLOBALS ['hp']"; 
mysqli_query( $conn, $sql4 );

请告诉我$ sql4 UPDATE有什么问题 一旦我获得points_for的支持,我就可以确保也可以添加和更新其他列。

0 个答案:

没有答案
相关问题