Jump to content

[SOLVED] Notice: Undefined index: id


Canman2005

Recommended Posts

Hi all

 

I have a website online which I have taken offline to work on it, but when I load the pages, I get the error

 

Notice: Undefined index: id in C:\www\web\connect\websitedata.php on line 5

 

The code for this websitedata.php page is shown below.

 

<?
$formaturl = explode('?',$_SERVER['REQUEST_URI']);
$url = $formaturl[0];

if ($_GET['id'] != '')
{
if ($_GET['page'] != '')
{
$webinfosql = "SELECT * FROM websitecontent WHERE `id` = '".$_GET['page']."'";
print $webinfosql;
}
else
{
$webinfosql = "SELECT * FROM websitecontent WHERE `id` = '".$_GET['id']."'";
print $webinfosql;
}
}
else
{
$webinfosql = "SELECT * FROM websitecontent WHERE `url` = '$url' AND (`level` = '1' || `level` = '0')";
print $webinfosql;
}
$webinfoquery = @mysql_query($webinfosql,$connection) or die(mysql_error());
while ($webinforow = mysql_fetch_array($webinfoquery))
{
$webinfoid = $webinforow['id'];
$webinfotitle = $webinforow['title'];
$webinfomastertitle = $webinforow['mastertitle'];
$webinfoimage = $webinforow['featureimage'];
$webinfoimage1 = $webinforow['image1'];
$webinfocontent = $webinforow['content'];
$webinfoadobereader = $webinforow['adobereader'];
}
?>

 

Can anyone see why I am getting this error?

 

Any help would be great

 

Thanks in advance

 

Dave

Link to comment
Share on other sites

When dealing with user input variables ($_POST and $_GET mainly) you should check that they exist first before using them. Doing if($_GET['myvar']) is not sufficient. As you are not checking whether that variable exists, instead you are checking its value.

 

What you should do is use isset(), like so:

if(isset($_GET['myvar']) && $_GET['myvar'] != ""))
{
    // variable exists and is not empty
}
else
{
    // variable doesn't exist and is empty
    // OR
    // variable exists but is empty
}

Now PHP will check the existence of the variable, if it exists it will check that the variable isn't empty. If the variable doesn't exist it will go straight to the else statement.

 

And no its not to do with register_globals.

Link to comment
Share on other sites

If you read my post you will see why.

 

Also you're code can be rewritten to this:

<?php

$formaturl = explode('?',$_SERVER['REQUEST_URI']);
$url = $formaturl[0];

$webinfosql = 'SELECT * FROM websitecontent WHERE ';

if (isset($_GET['id']) && !empty($_GET['id']))
{
    $webinfosql .= "`id` = '{$_GET['id']}'";
}
elseif (isset($_GET['page']) && !empty($_GET['page']))
{
    $webinfosql .=  "`id` = '{$_GET['page']}'";
}
else
{
     $webinfosql .= "`url` = '$url' AND (`level` = '1' || `level` = '0')";
}

print 'SQL Query: ' . $webinfosql;

$webinfoquery = mysql_query($webinfosql, $connection) or die(mysql_error());

while ($webinforow = mysql_fetch_array($webinfoquery))
{
    $webinfoid = $webinforow['id'];
    $webinfotitle = $webinforow['title'];
    $webinfomastertitle = $webinforow['mastertitle'];
    $webinfoimage = $webinforow['featureimage'];
    $webinfoimage1 = $webinforow['image1'];
    $webinfocontent = $webinforow['content'];
    $webinfoadobereader = $webinforow['adobereader'];
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.