【问题标题】:Undefined Index PHP未定义索引 PHP
【发布时间】:2014-11-19 17:30:27
【问题描述】:

我已经进行了一些搜索,但似乎无法找到答案。我是 PHP 新手。

这是我的 HTML 表单;

  <form id = "getGame" action="Form.php" method = "get">
    <label> Serial 
      <input type = "search" name = "serial" />
    </label>
    <label> Title
      <input type = "search" name = "title" />
    </label>
    <label> Year
      <select name="gameYear">
        <option value="1998">1998</option>
        <option value="1999">1999</option>
        <option value="2000">2000</option>
      </select>
    </label>
    <label> Price
      <input type = "search" name = "price" />
    </label>
    <input type = "submit" name="search" value = "Search">
  </form>

这是我的 PHP 页面;

include 'db_Conn.php';
$gameYear = $_GET ['gameYear'];
$sql = "SELECT serialNo, gameTitle, gameYear gamePrice FROM games  WHERE gameYear = '$gameYear'";
$Games = mysqli_query($conn, $sql) 
or die(mysqli_error($conn));

while ($row = mysqli_fetch_assoc($Games)) {
    $serial = $row['serialNo'];
    $title = $row['gameTitle'];
    $gameYear = $row['gameYear'];
    $gamePrice = $row['gamePrice'];     
    echo "<div>$serial, $title, $gameYear, $gamePrice</div>\n";
}

mysqli_free_result($Books); 
mysqli_close($conn);

?>

我的问题是,当我运行表单时,我收到以下消息“通知:未定义索引:gameYear”。但是,在下面的行中,它显示了我选择的游戏年份的所有记录。下面显示的示例;

Notice: Undefined index: gameYear in ____ on line 40
493928, Test Drive, 2002, $12.95

谢谢,对正确方向的任何一点表示赞赏。

【问题讨论】:

  • 您在 SELECT 语句中的 gameYear 后面缺少一个逗号。

标签: php html indexing undefined


【解决方案1】:

你错过了一个,。将其更改为:

$sql = "SELECT serialNo, gameTitle, gameYear, gamePrice FROM games  WHERE gameYear = '$gameYear'";

【讨论】:

  • 非常感谢!快把我逼疯了。我必须记得以后更仔细地扫描所有内容。再次感谢您,非常感谢。
  • @RonaIdo,如果 Ali 回答了您的问题,请相应地标记它,尝试使用 stackoverflow 规则 :-)
  • @mTorres 我有,最初不会让我因为我必须等待一段时间才能获得更多答案。
【解决方案2】:

如果在数据库中没有找到数据,它将产生 4 个错误。所以首先检查 mysqli_num_rows() 函数是否找到任何数据。

if (mysqli_num_rows($Games) > 0) {
    while ($row = mysqli_fetch_assoc($Games)) {
        $serial = $row['serialNo'];
        $title = $row['gameTitle'];
        $gameYear = $row['gameYear'];
        $gamePrice = $row['gamePrice'];
        echo "<div>$serial, $title, $gameYear, $gamePrice</div>\n";
    }
}

是的,当然你漏掉了一个逗号,所以 sql 应该是这样的

$sql = "SELECT serialNo, gameTitle, gameYear, gamePrice FROM games  WHERE gameYear = '$gameYear'";

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2015-03-24
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2015-01-20
    • 2014-12-19
    相关资源
    最近更新 更多