gpt4 book ai didi

PHP 未定义索引 - sortby

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

它基本上显示来自mysql数据库的数据并使用

$sortby = $_GET['sort'];

我得到的错误是

Notice: Undefined index: sort in /home/4507408/public_html/list.php on line 8

这是完整的代码,有什么想法吗? (第 8 行是 $sortby = $_GET['sort'];)

感谢观看:)

<?php
$dbhost = 'localhost';
$dbuser = 'CU4507408';
$dbpass = 'adamadam1';
$dbname = 'CU4507408';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to database");
mysql_select_db($dbname);
$sortby = $_GET['sort'];
?>

在页面顶部

<table border="1">

<tr>
<th><a href="list.php?sort=name">Product Name:</a></th>
<th><a href="list.php?sort=price">Price £</a></th>
<th><a href="list.php?sort=manufacturer">Manufacturer</a></th>
<th><a href="list.php?sort=rating">Rating</a></th>
<th><a href="list.php?sort=categoryname">Category</a></th>
</tr>
<?php
$query = "SELECT p.productID, p.name, p.price, p.manufacturer, p.rating, c.categoryname FROM product p INNER JOIN category c WHERE p.categoryID=c.categoryID ORDER BY $sortby ASC;";
$result = mysql_query($query) or die("failed!");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>

<td><a href="link.php?productID=<?= $row['productID'] ?>"><?= $row['name'] ?></a></td>
<td><?= $row['price'] ?></td>
<td><?= $row['manufacturer'] ?></td>
<td><?= $row['rating'] ?></td>
<td><?= $row['categoryname'] ?></td>
</tr>
<? } ?>

最佳答案

试试看:

$sortby = isset($_GET['sort']) ? $_GET['sort'] : 'default_value';

此外,如果您使用通过 $_GET 传递的变量,您必须检查它是否不是在您的查询中注入(inject)某些内容的值。好的做法是:

$sortbyValues = array('price', 'manufacturer', 'rating', 'categoryname');
$sortby = isset($_GET['sort']) && in_array($sortby, $sortbyValues) ? $_GET['sort'] : 'default_value';

关于PHP 未定义索引 - sortby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21044033/

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