Jump to content

notice : undefined index - problem


justin7410

Recommended Posts

Hey guys,

 

i am working on my site , and i have a global conditional that is pretty simple in what i want it to do ,

 

i have 4 variables that are not always set, i am writing an if elseif statement to see if whichever variable is used to then continue to its specified query/

 

if ($current_file == 'profile.php') {
    
    $mid = $_GET['male_id'];
    $fid = $_GET['female_id'];
    $serid = $_GET['series_id'];
    $id = $_GET['id'];


    if(isset($mid) === true){


       $query = "SELECT * FROM `content` WHERE `id` = $mid";
    
    }elseif (isset($sid) === true) {


       $query = "SELECT * FROM `content` WHERE `id` = $sid";
   
    }elseif(isset($serid) === true){


       $query = "SELECT * FROM `content` WHERE `id` = $serid";

    }elseif(isset($id) === true){


       $query = "SELECT * FROM `content` WHERE `id` = $id";
    } 

$info = mysql_query($query); while($row = mysql_fetch_assoc($info)){

    extract($row);

     }

}

now the conditional works fine , in terms of the query working and grabbing the desired data and parlaying that data onto the site.

 

My issue is simply , i want to leave error reporting on for my users, so that if there are errors or problems i dont know of , server side , they can report this to me.

 

When i do this, i get :

 

 Notice: Undefined index: movieid in /home/medshare/public_html/include/init.php on line 49

Notice: Undefined index: seriesid in /home/medshare/public_html/include/init.php on line 51

Notice: Undefined index: id in /home/medshare/public_html/include/init.php on line 52

 

any explanation as to why this is happening ?

 

edit: i understand as to "why" , the variable that is the $_GET is now set to $sid , and the 3 other variables are not defined with since they are not in the URL. should i set the variables deeper into the conditional ? are they too global to where the entire if statement which is looking for current file and need to be set in their own if statements ?

 

also , any suggestions on how to alter my code to provide the same results without the notice being portrayed ?

 

much appreciated guys

Edited by justin7410
Link to comment
Share on other sites

To eliminate the error -- and in my opinion this should be considered an error, not just a notice to be hidden -- you should check to see if the GET index is set. Something like:

    $mid = (isset($_GET['male_id']) ? $_GET['male_id'] : null);
    $fid = (isset($_GET['female_id']) ? $_GET['female_id'] : null);
    $serid = (isset($_GET['series_id']) ? $_GET['series_id'] : null);
    $id = (isset($_GET['id']) ? $_GET['id'] : null);
This will require you to change your IF statements, which you should do anyway since all of the variables are set now. You can use if (! empty($_GET['id'])) instead of isset()

 

Of course, you will need to add an ELSE block for when none of them is provided (something you should have in there now, anyway).

 

Having said that, it is NOT a good idea to allow any kind of PHP error message to be displayed on a public-facing website. This can provide hackers information you don't want them to have.

 

If you want information about errors that have occurred, log them to a file -- use the date as part of the filename -- then have a cron job email the file to you each day if it is not empty.

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.