Reputation: 1058
Its basically displaying data from mysql database and using
$sortby = $_GET['sort'];
And the error I get is
Notice: Undefined index: sort in /home/4507408/public_html/list.php on line 8
Here is the full code, any ideas? (Line 8 is $sortby = $_GET['sort'];)
Thanks for looking :)
<?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'];
?>
Thats at the top of the page
<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>
<? } ?>
Upvotes: 1
Views: 1150
Reputation: 326
Remove this line from your code
$sortby = $_GET['sort'];
Add this code in top of your code
if (isset($_GET['sort']) && !empty($_GET['sort'])) {
$sortby = $_GET['sort'];
}else{
/*in case value of $_GET['sort'] is not retrieved, Action can be done for error handling here. */
}
Upvotes: 0
Reputation: 2916
Problem is occurs because your index is not set on configuration file. You can use any of the following options:
@$sortby = $_GET['sort'];
$sortby = isset($_GET['sort']) ? $_GET['sort'] : 'default_value';
Upvotes: 1
Reputation: 152206
Just try with:
$sortby = isset($_GET['sort']) ? $_GET['sort'] : 'default_value';
Also if you use valriables passed with $_GET
you have to check if it is not a value that inject something in your query. Good practise is to:
$sortbyValues = array('price', 'manufacturer', 'rating', 'categoryname');
$sortby = isset($_GET['sort']) && in_array($sortby, $sortbyValues) ? $_GET['sort'] : 'default_value';
Upvotes: 5