1

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 ?

1
  • Always pass the id? I don't really get what is your problem. Commented Jul 28, 2010 at 7:23

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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;
}

Comments

0

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.

Comments

-1

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.

3 Comments

can any body explain me why this is low commented
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)
I think because this will still throw E_NOTICE error which he asks specifically how to avoid in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.