这里是 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/