tags:

views:

56

answers:

5

i have question for query string

i have page wehere i pass query string like that

      abc.php?id=12

some time query sting "id" is not passed like

      abc.php

and some time value of id is empty

     abc.php?id=

and in the above 2 possibilities pages gives error.

how to fix this ?

+2  A: 

Check if $_GET['id'] is set (using isset() ) before attempting to do anything with it.

Mchl
+2  A: 

If you check it correctly using isset() and empty() you should be able to avoid any problems.

spinon
A: 

check the below condition

if($_GET['id'] == '') {

 // Your Condition Here

} else if($_GET['id'] != '') {

 // Your Condition Here

} else if(isset($_GET['id'])) {

 // Your Condition Here

} 

Hope this Helps.

Fero
can any body explain me why this is low commented
Fero
@ Fero i dont know who low comment you...
air
I'd suspect it's because that code triggers an E_NOTICE error every time the id parameter is not passed to the script and you should always work with full error_reporting and not clutter it with avoidable error messages. (Just a guess, didn't vote)
edorian
@air: I know this is not a Procedure answer but this will fulfill your queries need
Fero
A: 

Applying checks using the isset() language construct will supress errors and allow you to access array keys that may not exist. Here is a small function for transparent existance of a value.

$myID = getInput('id'); // Returns 'null' if ID doesn't exist but throws no PHP errors
$myID = getInput('id', 50); // Returns 50 if your ID doesn't exist

With this you can perform validation checks to make sure you have an ID

if( ($myID = getInput('id', 0)) < 1) {
     die('Invalid ID Value');
}
die("My ID is: $myID");


function getInput($key, $default = null) {
    return isset($_GET[$key]) && $_GET[$key] != null ? $_GET[$key] : $default;
}
Paul Dragoonis
A: 

You should always validate the incoming data before using it. In this case use isset to test if the variable exists and has a value other than null and use ctype_digit to check if the value consists only of digit characters.

Gumbo