0

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I have searched around and can't figure this issue out.

$post   = (string) mysql_real_escape_string($_GET['post']);<br>
$page   = (int) mysql_real_escape_string(intval($_GET['page']));

I don't understand what the error undefined index:post means. I get this error for both the $post and $page lines.

Community
  • 1
  • 1
  • Is 'post' a valid get parameter? When you are performing your GET request, is there a '?post=something' appended to the URL? – Ethan Hayon Sep 11 '12 at 20:49

2 Answers2

1

The $_GET variable will be populated only if your URL has a query string (that usually means a form was submitted with the GET method).
So you must first check if $_GET contains values, then use theses values:

if(isset($_GET['post']) && isset($_GET['page'))
{
    $post   = mysqli_real_escape_string($_GET['post']);
    $page   = intval($_GET['page']);
}

Casting to string is useless in your first line of code: mysqli_real_escape_string returns a string anyway.
In your second line of code:

  • using mysql_real_escape_string since you are using intval: an integer value does not need any extra escaping
  • casting to int is useless, this was already done with intval

Important notice:

Since all mysql_* functions are deprecated and will be removed in a future version of PHP, you should use MySQLi or PDO (I used MySQLi in my code sample).

Community
  • 1
  • 1
Jocelyn
  • 11,209
  • 10
  • 43
  • 60
0

This error means exactly what the warning is telling you.

$_GET is an array of items passed in as HTTP GET parameters.

$_GET['page'] and $_GET['post'] are giving you undefined index warnings because there is no item in the associative array with the key 'post' or 'page'.

One way to figure out what is going on is to var_dump() the entire $_GET array to see what's in it.

var_dump($_GET)

Ethan Hayon
  • 346
  • 1
  • 9