0

多亏了这里乐于助人的人们的一些深刻的反馈,以及从其他来源获得的大量洞察力和指导,我在为我的青年田径项目创建时间表/排名数据库的过程中已经走了很长一段路。我已经完成了该过程的最后(尽管是最重要的)部分之一,并且可以使用您的帮助来识别我正在使用的语法中的缺陷,以允许学校报告游戏分数。

这是我用来完成这一切的时间表页面:

http://www.parochialathleticleague.org/schedules_test.html

内容是从存储在 MySQL 表中的联赛数据动态生成的。如果您查看该页面,您会看到可以单击一个图标来报告尚未记录的每场比赛的得分。当用户单击其中一个图标时,我想将该游戏的重要数据发送到单独的分数报告页面以进行更新。

以下是 PHP 脚本中的相关代码,它试图通过会话传递该数据:

if ($row['home_score'] == '0' && $row['away_score'] == '0') {
        session_start();
        $_SESSION['home_team'] = $row['home_score'];
        $_SESSION['away_team'] = $row['away_score'];
        $_SESSION['game_id'] = $row['game_id'];
        $_SESSION['league'] = "test_league";
        echo '<td><a href="report_score.html"><img src="images/report_icon.png" alt="Report Score" /></a></td>';
    } else {
        echo '<td>' . $row['home_score'] . '<br>' . $row['away_score'] . '</td>';
    } 

不幸的是,我很确定我没有正确编写代码,因为会话变量似乎根本没有发送。加载分数报告页面时,用户应该会看到会话生成的两个团队名称(主场和客场),但它们却完全丢失了。这是分数报告页面的链接,以获得更好的想法:

http://www.parochialathleticleague.org/report_score.html

此外,这是该页面中应该调用会话变量的代码:

<p class="p5 results">Enter Game Results</p>

<div class="form2">
<label><?php session_start(); echo $_SESSION['away_team']; ?></label>
<span>                                                    
<input type="text" name="away_team" />
</span>
</div>

<div class="form2">
<label><?php session_start(); echo $_SESSION['home_team']; ?></label>
<span>                                                    
<input type="text" name="home_team" />
</span>
</div>

最后,这是脚本本身的代码,它应该处理表单并相应地更新 MySQL 表:

<?php

// Connect to the database:
require ('../mysqli_connect.php');

// Start the session:
session_start();

// Validate the school:
if (empty($_POST['school'])) {
    echo "You forgot to enter your school.<br>";
    $validate = 'false';
} elseif ($_POST['school'] != ($_SESSION['home_team'] || $_SESSION['away_team']) {
    echo "Your school does not match one of the two on file for this game.<br>";
    $validate = 'false';
} else {
    $school = mysqli_real_escape_string($db, trim($_POST['school']));
    $validate = 'true';
}

// Validate the password:
if (empty($_POST['pass'])) {
    echo "You forgot to enter your password.<br>";
    $validate = 'false';
} else {
    $pass = mysqli_real_escape_string($db, trim($_POST['pass']));
    $validate = 'true';
}

// Validate the away score:
if (empty($_POST['away_team'])) {
    echo "You forgot to enter the away score.<br>";
    $validate = 'false';
} elseif (!is_numeric($_POST['$away_team']) || $_POST['$away_team'] < 0 ) {
    echo "You entered an invalid score for the away team.<br>";
    $validate = 'false';
} else {
    $away_score = mysqli_real_escape_string($db, trim($_POST['away_score']));
    $validate = 'true';
}

// Validate the home score:
if (empty($_POST['home_team'])) {
    echo "You forgot to enter the home score.<br>";
    $validate = 'false';
} elseif (!is_numeric($_POST['$home_team']) || $_POST['$home_team'] < 0 ) {
    echo "You entered an invalid score for the home team.<br>";
    $validate = 'false';
} else {
    $home_team = mysqli_real_escape_string($db, trim($_POST['home_score']));
    $validate = 'true';
}

// If all conditions are met, process the form:
if ($validate != 'false') {
    $q1 = "SELECT school_id FROM user_schools WHERE (school_name='$school' AND pass='$pass')";
    $r1 = mysqli_query($db, $q1);
    $num = mysqli_num_rows($r1);
    if ($num == 1) {
        // Get the row for the game ID:
        $row = mysqli_fetch_array($_SESSION['game_id'], MYSQLI_NUM);
        // Perform an UPDATE query to modify the game scores:
        $q2 = "UPDATE" . $_SESSION['league'] . "SET home_score='$home_score', away_score='$away_score' WHERE game_id=$row[0]";
        $r2 = mysqli_query($db, $q2);
        if (mysqli_affected_rows($db) == 1) {
            header("Location: schedules_test.html");
        } else {
            echo "The scores could not be reported due to a system error. Apologies for the inconvenience. If this problem continues, please contact us directly.";
        }
    } else {
        echo "Your school and password combination do not match those on file for this game. If you forgot your password, please contact Will Bryant directly.";
    }       

}

mysqli_close($db);

?>

很抱歉扔了这么多代码。我一直在快速学习这些语言,并尽我所能接受尽可能多的指导。我相信我在这个过程中朝着正确的方向前进,但无疑在语法上犯了一些错误(轻微或重要,我不完全确定)。

单击提交按钮会导致一个空白的白色页面,但已经足够清楚,当团队名称无法加载到报告页面上时存在问题。

在上面的示例中,我到底哪里出了问题,特别是与传递会话数据有关?任何和所有的建议、洞察力、批评、点名等都将不胜感激!

4

1 回答 1

1

虽然很难同时理解这样一堵代码墙,但我有一种强烈的感觉,你不是故意使用会话。尝试使用其他人使用的常规方法重写您的代码:

每个必须显示存储在数据库中的数据的页面,都必须通过 GET 参数接收行标识符,并使用它查询数据库。

虽然会话(在您的情况下)只能用于授权目的。

于 2013-09-04T16:44:46.553 回答