-4

Why am I having undefined index errors here on my code when I add the sort feature? These are the errors I'm getting

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 58

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 61

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 64

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 67

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 70

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 73

Notice: Undefined index: sort in C:\xampp\htdocs\how are things\admin panel\view.php on line 76

This is the code on the lines 58 61 64 67 70 73 76

$result = "SELECT * FROM customers";
    if($_GET['sort'] == 'first_name'){
     $result .= " ORDER BY first_name";
    }
   else if($_GET['sort'] == 'last_name'){
     $result .= " ORDER BY last_name";
    }
     else if($_GET['sort'] == 'address'){
     $result .= " ORDER BY address";
    }
     else if($_GET['sort'] == 'phone_number'){
     $result .= " ORDER BY phone_number";
    }
     else if($_GET['sort'] == 'email'){
     $result .= " ORDER BY email";
    }
     else if($_GET['sort'] == 'city'){
     $result .= " ORDER BY city";
    }
     else if($_GET['sort'] == 'country'){
     $result .= " ORDER BY country";
    }
$result = mysql_query($result) or die(mysql_error());
vascowhite
  • 18,120
  • 9
  • 61
  • 77
alexzandria
  • 39
  • 1
  • 5
  • 4
    Welcome to Stack Overflow! Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Instead you should learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you pick PDO [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – War10ck Apr 15 '13 at 17:57
  • use `if(isset($_GET['sort']) && $_GET['sort']== 'someval')` –  Apr 15 '13 at 17:58
  • see [PHP Undefined Index](http://stackoverflow.com/questions/4842759/php-undefined-index) – kero Apr 15 '13 at 17:58
  • 1
    I am just curious -- a lot of people seem to ask this question -- but, exactly what do you not understand about `Undefined index: sort`? You use `$_GET['sort']`... you get a notice saying that it is undefined... and... nothing? – Sverri M. Olsen Apr 15 '13 at 18:32

3 Answers3

2

you should check if sort is set and then use it. maybe something like this would work for you:

    $allowedSorts = array('first_name', 'last_name', 'address','phone_number', 'email', 'city', 'country');

    $sort = isset($_GET['sort']) ? $_GET['sort'] : '';
    $result = "SELECT * FROM customers";
    if(in_array($sort, $allowedSorts)){
         $result .= " ORDER BY {$sort}";
    }

    $result = mysql_query($result) or die(mysql_error());
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
1

You're not first checking if the sort index exists before checking its value.

Try writing it as:

if (isset($_GET['sort'])) {
    switch ($_GET['sort']) {
        case 'first_name':
            $result .= ' ORDER BY first_name';
            break;
        case 'last_name':
            $result .= ' ORDER BY last_name';
            break;

        // etc...
    }
}

As an aside, mysql_* functions are deprecated. Look into using PDO.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
0

The "errors" you are getting are not really errors. They are notices. It means PHP is notifying you that something may be wrong.

In this case, PHP is telling you that the variable that you are trying to access in the $_GET-array, does not exist.

There are several ways around this.

  • Tell PHP no longer to notify you when things seem wrong:

    //From this point on, show all errors except notices.
    error_reporting(E_ALL ^ E_NOTICE);
    

    However, it is better to actually fix your code.

  • As pointed out by many of the other answers, you can prevent your code from trying to access the non-existent variable, by first making it check if it actually exists.

    //Checks for the existence of any variable.
    isset($_GET['sort']);
    
    //Check if the specified array has a given key.
    array_key_exists('sort', $_GET);
    

    Both work.

In your case, the answer given by @Omar Jackman would be an elegant solution.

Community
  • 1
  • 1
Avaq
  • 3,009
  • 1
  • 21
  • 25