1

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>
<? } ?>
3
  • 1
    mysql extension is deprecated, instead use mysqli or PDO_MySQL . Commented Jan 10, 2014 at 12:13
  • Where do you get the sort from? Seems to be not send or defined properly there. Do an echo $_REQUEST to see how that looks like. ;) Commented Jan 10, 2014 at 12:13
  • When you load page, sort is not defined and this is what spits error. Do check and create default value. As suggested by hsz. Commented Jan 10, 2014 at 12:14

3 Answers 3

5

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';
Sign up to request clarification or add additional context in comments.

2 Comments

@user3181679 If you find my answer correct, remember to accept it with the tick on the left.
I will do, you answered it that fast that I can't yet haha! I will tho :)
1

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';

3 Comments

Did you copy my answer ? ;-)
I have give both options to use still I can mention your credit there :)
However using @ is not a good practice. You should avoid errors/notices instead of ignoring them.
0

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. */
    }

1 Comment

because of we can define other value if GET is empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.