Page 1 of 1

Undefined index

Posted: Mon Mar 22, 2010 11:33 am
by gfreak000
I keep getting this error when i run my webpage on my localhost.

Notice: Undefined index: pid in C:\wamp\www\index.php on line 6

This is my code for this part:

//This is the body.............................................
if(!$_GET['pid']){ <===== LINE 6
$pageid='1';
}else{
$pageid=$_GET['pid'];
}
//Query the body..............................................................
$sqlCommand="SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1";
$query=mysqli_query($mysqlcon,$sqlCommand) or die(mysqli_error());
while($row=mysqli_fetch_array($query)){
$Body=$row["pagebody"];
}
mysqli_free_result($query);

//End of body..........................................................................

I dont understand why it says undefined since i thought if there was no get pid then it would automatically make pageid=1

The script runs fine when i click on a link to define the pid but when i just go straight to the script from localhost i get this notice.

Re: Undefined index

Posted: Mon Mar 22, 2010 11:41 am
by scarface222
try just saying if(!$_GET) instead because if that variable is not url then it does not exist.

Re: Undefined index

Posted: Mon Mar 22, 2010 12:08 pm
by gfreak000
Well i need the $_GET because when a user clicks on a link its supposed to get a pid through mysql and render onto the page what i have stored in mysql database table.


it also gives me another error when i just use $_GET.

Re: Undefined index

Posted: Mon Mar 22, 2010 12:11 pm
by scarface222
well this works fine for me

if ($_GET){
$sort=mysql_real_escape_string($_GET['sort']);
$type=mysql_real_escape_string($_GET['type']);
}
else {
$sort=0;
$type=0;
}

if not then just put @$_GET to suppress the error, since that is the only problem, there is no syntax issue.

Re: Undefined index

Posted: Mon Mar 22, 2010 4:02 pm
by AbraCadaver
scarface222 wrote:if not then just put @$_GET to suppress the error, since that is the only problem, there is no syntax issue.
Nooooo... If there is no id in the query string of the request URL then it won't be SET, and so you get an notice because there is no $_GET['id']. Do it the proper way.

Code: Select all

if(!isset($_GET['pid']))

Re: Undefined index

Posted: Mon Mar 22, 2010 5:02 pm
by scarface222
oops forgot about that haha, sorry.