1

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

first time asking questions here. I am trying to create a small, primitive forum. I am basing it on this: while making changes to it where it is needed.

So far, I have setup the database and all that. Now I am trying to fix the view_topic.php and it is returning "Notice: Undefined index: id" on line 30 something. This is line 30: $id=$_GET['id'];

Community
  • 1
  • 1
user1868565
  • 141
  • 1
  • 3
  • 8

3 Answers3

2

You should make sure id is a valid index before accessing it.

$id = isset($_GET['id']) ? $_GET['id'] : null;
FThompson
  • 28,352
  • 13
  • 60
  • 93
  • was just about to type this same answer. +1 – Ryan Dec 01 '12 at 18:44
  • 1
    No, all you've done is moved the problem elsewhere, since we have not been given a confirmation that the lack of this value is an acceptable case. Since `id` is a querystring parameter in the OP's example, I'm going to go ahead and suggest that it is not. – Lightness Races in Orbit Dec 01 '12 at 18:45
  • It stopped the error message, but as @LightnessRacesinOrbit said its returning nothing, I gave up on adding my code to stackoverflow, here is pastebin: http://pastebin.com/g7QC0kDv – user1868565 Dec 01 '12 at 18:55
  • @user1868565: I already instructed you not to post "whole code", but instead your _complete, minimal example_. – Lightness Races in Orbit Dec 01 '12 at 18:56
  • @LightnessRacesinOrbit I understand, but I guess the whole code might be useful here? The "$id" is being used at several occasions, the connection part goes fine. – user1868565 Dec 01 '12 at 19:00
  • @user1868565: The whole code is completely useless when you have narrowed down your problem to an isolated five/six-line program. There is zero reason that _we_ need to extract the problem from known-working SQL-related code; _you_ should do that work for us (well, it's still for _you_!) – Lightness Races in Orbit Dec 01 '12 at 19:02
  • @LightnessRacesinOrbit: I see what you mean now, well it's from line 21-26 on that pastebin link. I changed it to what Vulcan posted and the error message does not appear anymore, but now it is not returning anything whilst echoing it. – user1868565 Dec 01 '12 at 19:05
  • @user1868565: That's for the reason that I wrote in my first comment to this answer. – Lightness Races in Orbit Dec 01 '12 at 19:13
1

Notice: Undefined index:

This means that a piece of code attempts to access an element of an array that does not exist.

So, for example:

$myArray = Array();  // an empty array
echo $myArray['id']; // print 'id' element

// ^^ Oops! No such element yet!

The $_GET array contains the arguments from the querystring, which is the ?id=1 part of your request URL.

You will need to find out why this does not contain the element with key 'id' when id was provided in the URL, and then make your code decide what to do in the case that this value is missing, using a function such as isset or array_key_exists and an if statement.

Most likely you will present a more useful error message and terminate the script, if your code cannot continue without a valid id value.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • **Pedant alert:** `isset` is a language construct, not a function; however, it is documented as a function since it is damn well close enough. – Lightness Races in Orbit Dec 01 '12 at 18:50
  • The `$_GET` array would also contain *all* form data *if* the form used `method="get"`. – toxalot Dec 01 '12 at 20:29
  • @toxalot: That's because a form with `method="get"` leads to a URL with the form data emplaced in the querystring. – Lightness Races in Orbit Dec 01 '12 at 21:05
  • *I* understand that, but a new programmer *may not*. It *leads to* a URL with a query string, but that doesn't mean the form was accessed with this query string. I think new programmers find this confusing. FYI, I agree with (and upvoted) your answer and comments. I was merely attempting to clarify. – toxalot Dec 01 '12 at 23:29
  • What do you mean "accessed with this query string"? It's important to use proper terminology for new programmers so that what they are learning is accurate, and I think your comments are a backwards step in that goal. – Lightness Races in Orbit Dec 01 '12 at 23:59
  • I'm not sure what terminology to use. As a self-taught programmer I often lack the terminology. I often know *how* to do something, but don't know what to *call* it. I'll try to explain. If I have a static form (for example) and visit it with the URL `http://example.com/form.html`, then I have "accessed it without a query string". But after I submit that form, then it may lead to a URL like `http://example.com/process_form.php?id=123`. Does that make sense? Is there a simple way to explain that without the example? – toxalot Dec 02 '12 at 00:19
  • @toxalot: Sure; like this: a form with `method="get"` leads to a URL with the form data emplaced in the querystring. This is in fact the URL used to _submit_ that form data and the fact that you can still see it once the _response_ page has loaded is frankly little more than an after-effect; though due to the use of `GET` this is a semantically dubious way of looking at things -- forms really should not use this method. – Lightness Races in Orbit Dec 02 '12 at 00:21
  • :) OK. I perceived your first reply to me as an implication that my statement was irrelevant. I was trying to explain why it wasn't. But I see now that your response completes my original reply. – toxalot Dec 02 '12 at 00:45
0

Are you sure id= exists in the URL? Try adding the following near the top of your code.

var_dump($_GET);

var_dump dumps information about a variable to your screen. In this case, it will display all the GET data sent from your form. This is a debugging technique so you can see if your code is receiving what you expect. If the form uses method="post" and id is a field in the form, then you will need to use $_POST['id'] or $_REQUEST['id'] to access it.

toxalot
  • 11,260
  • 6
  • 35
  • 58