Here's a php code
if(isset($_GET['id'])) {
//do something
} else {
redirect('index.php'); //redirect is a function
}
Now if id is set (ex: index.php?id=12) then the action is performed but if id is not set (ex: index.php?id=) this shows an error, how to overcome this...??
How to determine id is an integer and it's not empty and then perform the specific action....
Edited
Thank you all for your answers but i am still getting that error...
if(isset($_GET['id'])) { // I implemented all these codes but still....
$user= User::find_by_id($_GET['id']);
// Database Classes Fetches user info from database
}
else {
redirect('index.php'); //redirect function
}
If the id is 1 or greater than 1 then the script executes perfectly. ( index.php?id=1 )
But id i set id as noting i get an error i..e index.php?id=
The code should automatically redirect user to the index.php page instead of showing an error.....
ERROR : Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1
<? php //Method 1 if(! empty($_GET)) echo "exist"; else echo "do not exist"; //Method 2 echo "<br>"; if($_GET) echo "exist"; else echo "do not exist"; //Method 3 if(count($_GET)) echo "exist"; else echo "do not exist"; ?>
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.
We can use empty() function to check whether a string is empty or not. The function is used to check whether the string is empty or not. It will return true if the string is empty.
You should check both the set-status and the content:
if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) )
// ....
Note that you should not use the value simply as it is and work with it. You should make sure that it only contains values you expect. To check for an integer, and even better to work with an integer from that on, do something like that:
$id = ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? intval( $_GET['id'] ) : 0;
if ( $id != 0 )
// id is an int != 0
else
redirect('index.php');
What does the error tell you?
But this would check if it's set, and if it's an integer
if(isset($_GET['id']) && ctype_digit($_GET['id']) && intval($_GET['id']) > 0)
is_numeric @ php.net
Or check out ctype_digit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With