php - 使用 GET 函数时通过 MySQL 数据库未定义索引

标签 php mysql

这里是 PHP 菜鸟。我已经写出了尝试从数据库获取数据的 8 个步骤,但我的第一行只是返回

"Notice: Undefined index: itemid in C:\xampp\htdocs\1605267B\KUSO\cart.php on line 130"

我在第一行使用 GET 函数没有发现任何问题。请帮助我。

<?php

$itemid = $_GET['itemid'];

//TODO 1: Connect to forumdb database
$mysqli = new mysqli("localhost","root",null,"kuso");

//TODO 2: Prepare the statement to select message id and subject info belonging to $userid from forummessage table 
$stmt = $mysqli->prepare("select itemid, itemname, itempicture, description, price from items where itemid=?");

//TODO 3: Bind the values   
$stmt->bind_param("i",$itemid);

//TODO 4: Execute the statement
$stmt->execute();

//TODO 5: bind results into $messageid, $subject
$stmt->bind_result($itemid, $itemname, $itempicture, $description, $price);

echo "<table border='1'>";
echo "<tr><td><b>Subject</b></td><td><b>Actions</b></td></tr>";

// Use while loop to fetch messages and put in a <table>
while ($stmt->fetch()) {
    echo "<tr>";
    //TODO 6: In 1st <td>, display subject
    echo "Sliim-fit.jpg";
    echo "<a href='menschoice.php?productid==$itemid'>";

    //TODO 7: In 2nd <td>, display View hyperlink with messageid in query string.
    //The View hyperlink links to messagedetails.php
    echo "<td> <a href='messagedetails.php?messageid=$messageid'> View </a></td>";

    echo "</tr>";
}

echo "</table>";


//TODO 8: close the statement
$stmt->close();

//TODO 9: close $mysqli
$mysqli->close();

?>

我不知道为什么它总是让我返回 undefined index 。请前辈帮助我,谢谢。

最佳答案

更改以下行

$itemid = $_GET['itemid'];

$itemid = !empty($_GET['itemid'])  ? $_GET['itemid'] : 0;

任何默认 ID 都应为零,如果您不想显示任何匹配项,则应为 0。

关于php - 使用 GET 函数时通过 MySQL 数据库未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917647/

相关文章:

php - mustache 部分和代码重用

php - Xpath 代码在 firepath 中有效,但在 php 脚本中无效

mysql - 将特定列中具有相同值的行分组

Mysql 一个表与另外两个表基于标志列进行联接

php - 数组在 ajax post 上被切断。 Ajax发帖限制?

php - 使用 ZendFramework 2 + Doctrine 2 在分页集合中使用水合器更改对象类型

php - 带有 SSL 套接字的 CodeIgniter 和 Gmail SMTP 超时

php - 如何从datalist中获取值id并显示标题?

PHP 不插入 MySQL 数据库,所有语句都在那里

php - 将联合标签搜索与 RT sphinx 中的其他参数相结合