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.
Undefined index
Moderator: General Moderators
-
scarface222
- Forum Contributor
- Posts: 354
- Joined: Thu Mar 26, 2009 8:16 pm
Re: Undefined index
try just saying if(!$_GET) instead because if that variable is not url then it does not exist.
Re: Undefined index
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.
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
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.
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.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Undefined index
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.scarface222 wrote:if not then just put @$_GET to suppress the error, since that is the only problem, there is no syntax issue.
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
oops forgot about that haha, sorry.