Undefined index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gfreak000
Forum Newbie
Posts: 3
Joined: Mon Mar 22, 2010 11:24 am

Undefined index

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Undefined index

Post by scarface222 »

try just saying if(!$_GET) instead because if that variable is not url then it does not exist.
gfreak000
Forum Newbie
Posts: 3
Joined: Mon Mar 22, 2010 11:24 am

Re: Undefined index

Post 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.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Undefined index

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Undefined index

Post 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']))
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: Undefined index

Post by scarface222 »

oops forgot about that haha, sorry.
Post Reply