MP145 Posted November 13, 2008 Share Posted November 13, 2008 This my code <?php if(!$_GET['ID']){ //Sample URL 1 : /news.php mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database"); $db = mysql_select_db("$dbname") or die("Unable to select db"); $data = mysql_query("SELECT * FROM newsitem,newsimage WHERE newsitem.newsID = newsimage.newsID ORDER BY newsitem.date DESC LIMIT 1 ") or die(mysql_error()); } else { $id = $_GET['ID']; // Sample URL 2 : /news.php?ID=2 mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database"); $db = mysql_select_db("$dbname") or die("Unable to select db"); $data = mysql_query("SELECT * FROM newsitem,newsimage WHERE newsitem.newsID = '$id' AND newsimage.newsID = '$id'") or die(mysql_error()); } while($info = mysql_fetch_array( $data )) { //All the data which is too long to post here } ?> When i click the Sample URL 2 (../news.php?ID=2) , all works fine but on Sample URL 1 (../news.php) i get the error Undefined index: ID but it shows the data from the database. Sample URL 1 - Displays the latest news Sample URL 2 - Display news based on the newsID How can i solve the error msg? Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/ Share on other sites More sharing options...
darkfreaks Posted November 13, 2008 Share Posted November 13, 2008 if you still get errors let us know <?php $id = $_GET['ID']; if(!$id||$id==""||empty($id)){ //Sample URL 1 : /news.php mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db = mysql_select_db("$dbname") or die("Unable to select db"); $data = mysql_query("SELECT * FROM newsitem,newsimage WHERE newsitem.newsID = newsimage.newsID ORDER BY newsitem.date DESC LIMIT 1 ") or die(mysql_error()); } else { $id = $_GET['ID']; // Sample URL 2 : /news.php?ID=2 mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db = mysql_select_db("$dbname") or die("Unable to select db"); $data = mysql_query("SELECT * FROM newsitem,newsimage WHERE newsitem.newsID = '$id' AND newsimage.newsID = '$id'") or die(mysql_error()); } while($info = mysql_fetch_array( $data )) { //All the data which is too long to post here } ?> Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689695 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 Thanks for the reply darkfreaks I've changed it but now it displays Undefined variable: id on both URL 1 and URL 2 Any other method? ??? Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689712 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 OK, Sorry copy and paste error i missed the $id = $_GET['ID']; on the 1st line, corrected it but still get the same msg in URL 1 Undefined index: ID and URL 2 works fine. Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689721 Share on other sites More sharing options...
darkfreaks Posted November 13, 2008 Share Posted November 13, 2008 what line does it say undefined index is at? usually it iwll say undefined index on line: number Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689725 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 On line 2 <?php $id = $_GET['ID']; if(!$id||$id==""||empty($id)){ //Sample URL 1 : /news.php <---LINE 2 //the rest ?> Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689732 Share on other sites More sharing options...
darkfreaks Posted November 13, 2008 Share Posted November 13, 2008 add another || then after $id=="false" Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689738 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 It still shows the same Undefined variable: id on line 2 Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689742 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 It works if i link to URL 1 using : ../news.php?ID= but not when i use ../news.php How do i go about it? ( prefer ../news.php ) Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689768 Share on other sites More sharing options...
Gighalen Posted November 13, 2008 Share Posted November 13, 2008 You could just edit your php.ini file to not show all errors. The script is trying to declare the ID variable but has null as a value, since ID does not exist. The error you're recieving is actually a warning, so bumping your php.ini file down a few error levels should fix it. Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689769 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 Thanks for the post Gighalen. But since iam running the page on localhost i can edit the php.ini file but i don't think my hosting company allows the request to edit their ini file. Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689774 Share on other sites More sharing options...
Gighalen Posted November 13, 2008 Share Posted November 13, 2008 Some hosts will do it for you upon request. You can always check to see if the ID is set before trying to declare the variable: if(isset($_GET['id'])){ yada yada yada } But if your host will edit your php.ini file for you, you simply need to change error_reporting = E_ALL to error_reporting = E_ERROR Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689776 Share on other sites More sharing options...
MP145 Posted November 13, 2008 Author Share Posted November 13, 2008 YES!!!!!!! Thanks Gighalen!! and darkfreaks. Manage to do it..change the script order and added the isset on top. Here's the working code. <?php if(isset($_GET['id'])){ $id = $_GET['ID']; mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db = mysql_select_db("$dbname") or die("Unable to select db"); $data = mysql_query("SELECT * FROM newsitem,newsimage WHERE newsitem.newsID = '$id' AND newsimage.newsID = '$id'") or die(mysql_error()); } else { mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database".mysql_error()); $db = mysql_select_db("$dbname") or die("Unable to select db"); $data = mysql_query("SELECT * FROM newsitem,newsimage WHERE newsitem.newsID = newsimage.newsID ORDER BY newsitem.date DESC LIMIT 1 ") or die(mysql_error()); } while($info = mysql_fetch_array( $data )) { //As Gighalen mentioned "yada yada yada" } ?> Now it works!!!! Link to comment https://forums.phpfreaks.com/topic/132630-solved-get-function-error-undefined-index/#findComment-689803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.