Canman2005 Posted March 3, 2007 Share Posted March 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted March 3, 2007 Author Share Posted March 3, 2007 I have seen that it might be something to do with register globals. Would this be the case and if so, what do I need to change? Never had to deal with this register globals issue before Thanks Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 3, 2007 Share Posted March 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted March 3, 2007 Author Share Posted March 3, 2007 I have found if you replace if ($_GET['page'] != '') with if (isset($_GET['page']) != '') then it seems to work, is this the case? Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted March 3, 2007 Author Share Posted March 3, 2007 Great, thanks Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 3, 2007 Share Posted March 3, 2007 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']; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.