【问题标题】:PHP/MySQL - Trying to Pass Query Variables From MySQL Fetch Array Through Page URLPHP/MySQL - 试图通过页面 URL 从 MySQL Fetch 数组中传递查询变量
【发布时间】:2013-09-04 20:40:59
【问题描述】:

我正在运行一个脚本,该脚本从 MySQL 表中获取一行,然后应该将该行中的某些变量传递到下一页,以便在允许用户更新的表单中使用。

这是试图将变量传递到下一页的脚本的摘录:

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

如果我在 href 中省略 "&game_id=" 之后的所有内容,则显示正常。但是,一旦我开始添加变量,它就会切断功能并停止显示页面。

我是否在语法上做了一些简单的错误?我试过用不同的方法来写它,但无济于事。我需要使用 http_build_query() 来完成这项工作吗?

如果您需要更多信息,这里是完整的脚本代码:

<?php

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

// Make the query for games from the schedule database and determine the game location:
$q = "SELECT tl.game_date, tl.game_time, tl.away_team, tl.home_team, tl.home_score,tl.away_score, tl.arbiter_id, us.football_location, us.football_map 
FROM test_league tl
    INNER JOIN user_schools us ON (tl.home_team = us.school_name)
ORDER BY tl.game_id";
$r = mysqli_query($db, $q);

// Declare two variables to help determine the background color of each row:
$i = 0;
$bgcolor = array('row1', 'row2');

// Begin function to print each game as a row:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

    echo '<tr class="' . $bgcolor[$i++ % 2] .'"><td>' . $row['game_date'] . '</td><td>' . $row['game_time'] . '</td><td class="alignleft"><a href="">' . $row['away_team'] . '</a> vs<br><a href="">' . $row['home_team'] . '</a></td>';

    // Determine if the score has been reported:
    if ($row['home_score'] == '0' && $row['away_score'] == '0') {
        echo '<td><a href="report_score.html?league=test_league&game_id=" . $row['game_id']"><img src="images/report_icon.png" alt="Report Score" /></a></td>';        
    } else {
        echo '<td>' . $row['home_score'] . '<br>' . $row['away_score'] . '</td>';
    }

    echo '<td><a href="' . $row['football_map'] . '" target="_blank">' . $row['football_location'] . '</a></td><td><a href="">' . $row['arbiter_id'] . '</a></td></tr>';

}

mysqli_free_result ($r);

mysqli_close($db);

?>

非常感谢任何和所有建议!

【问题讨论】:

  • 尝试 urlencode 在将变量添加到 href 时对其进行编码
  • 您在$row['game_id'] 之后缺少用于字符串连接的.
  • 而不是" . $row['game_id']",应该是:game_id='.$row['game_id'].' 单引号和缺少点。

标签: php mysql arrays get


【解决方案1】:

试试这个,你的语音标记和点混淆了:)

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

希望这会有所帮助!

【讨论】:

  • 感谢大家的反馈。我弄错并省略了我试图在代码中传递的大部分字符串(因此,缺少点),但真正的问题是使用双引号而不是单引号。更正解决了问题。非常感谢!
【解决方案2】:
$url = 'www.example.com?' . http_build_query($row,'','&amp;');

【讨论】:

    猜你喜欢
    • 2015-04-30
    • 2016-02-02
    • 2013-05-02
    • 2012-10-21
    • 1970-01-01
    • 2023-03-11
    • 2017-01-17
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多