gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:29:11 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com