Jump to content

[SOLVED] GET Function Error : Undefined Index


MP145

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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!!!!  ;D

 

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.