Notice: Undefined index with used isset and $_GET - php

this is my code
if(isset($_GET['delt']) || isset($_GET['editt'])){
$delt = intval($_GET['delt']);
$editt = intval($_GET['editt']);
}
when run the code I get the error
Notice: Undefined index: delt in
/opt/lampp/htdocs/tel-s/admin/top_a.php on line 116
I'm confused because i use isset and test used empty but do get the same problem

Your are testing only if one of params is set and later use both of them no matter if it's actually set.
Use && (and) instead of || (or):
if (isset($_GET['delta']) && isset($_GET['editt']))
OR
if (isset($_GET['delta'], $_GET['editt']))

Check both of variables for exists or change code
if( isset($_GET['delt']) ) {
$delt = intval($_GET['delt']);
}
if ( isset($_GET['editt']) ) {
$editt= intval($_GET['editt']);
}

It must have gotten $_GET['editt'] but couldn't get $_GET['delt'] and threw an error because it couldn't set anything to $delt. Consider #Justinas' answer.

did your url contains both "delt" and "editt" parameters ?
what is the out put of print_r($_GET) ?
isset function checks whether a variable is set or not ?
if you pass a string to intval() it will return 0

Related

PHP - proper check if $_POST['variable'] is posted

I want to check if $_POST['submit'] is posted.
My original code was:
if ($_POST['submit']) { }
But I have a PHP notice with this code - "Undefined index: submit in..."
So to remove the notice I have to write this:
if (isset($_POST['submit'])) { }
But this is pointless because $_POST array is global and it return always true.
Also if I want to check if $_POST['submit'] is not 0 without PHP notice I have to write this:
if (isset($_POST['submit']) && $_POST['submit'] != 0) { }
In this particular case I prefer:
if ($_POST['submit']) {}
But here I get the PHP notice.
So which way is the most proper/accepted?
Thank you
isset($_POST['submit']) checks if the submit key is set in the $_POST array. It doesn't just check whether the $_POST array exists and is therefore not "pointless". If you want to check whether the value is not falsey (== false), which includes 0, without triggering an error, that's what empty is for:
if (!empty($_POST['submit']))
which is the same thing as
if ($_POST['submit'])
but without triggering a notice should the value not exist.
See The Definitive Guide To PHP's isset And empty for an exhaustive explanation.
Try
if ($_SERVER['REQUEST_METHOD']=='POST')
{
//do
}
As of PHP version 7 there is a new method available called "Null Coalesce Operator". This method saves you time and space in your code.
$submit = $_POST['submit'] ?? '';
If submit key does not exist or is NULL the value of the $submit variable will be an empty string.
This method is not limited to $_POST. It will work for any variable. For example:
echo $my_message ?? 'No message found';
If message was not initialized or is NULL, the statement above will output No message found
There's even more that you can do with a null coalesce operator, documentation link below.
Documentation here
$_POST[] checks to see if a variable is submitted and not the form name.
if ( isset($_POST['name']) ) {
// work here
}
As per my understanding, It should be like below:
if (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']=='POST'){
# if method is post, code goes here.
}
and if you are sure that your method is POST for sure. and you have data post in $_POST you can use code like below:
if (isset($_POST['submit']) && $_POST['submit'] != '') {# I think, '' instead of 0
# if data is posted, code goes here.
}
I usually prefer $_POST.

PHP - checking isset on a $_SESSION[$_REQUEST[]] variable

Seems an easy one, but cannot work out why:
if(!isset($_SESSION[$_REQUEST["form_id"]]))
{
//do stuff
}
reutrns
Notice: Undefined index: form_id
empty returns same response.
This has been driving me mad for a while. :)
You're calling isset for $_SESSION but as the error states the issue is with $_REQUEST['form_id'] not being set.
if (!isset($_REQUEST['form_id']) || !isset($_SESSION[$_REQUEST['form_id']])) {
That's because it resolves $_REQUEST['form_id'] first and that causes the notice. You could do this instead:
if (!isset($_REQUEST['form_id']) || !isset($_SESSION[$_REQUEST["form_id"]]))
{
//do stuff
}
please check if key exists with
array_key_exists('form_id', $_REQUEST);
before checking value with
isset($_REQUEST['form_id']);
or check if your params are empty like
<?php
if (!empty($_REQUEST['form_id'])) {
// do anything
}
else
{
// I can't find the key in array
}
?>

I am looking for a php function that will prevent this: Undefined index: message in

if(defined($_POST["message"]) && defined($_POST["name"]))
{
In that if block an exception is thrown constantly. I am looking for a function that will prevent this by testing if it the post variable exists..if it doesnt exists return false instead of throwing an exception
if (isset($_POST["message"]) && isset($_POST["name"])) {
// ....
}
or if you want to check them to be not only set but also non-empty:
if (!empty($_POST["message"]) && !empty($_POST["name"])) {
// ....
}
Use isset() for determining if a variable is set. Use defined() to check if a constant is defined with the define() function.
See the Manual:
defined function
isset function
Use isset instead of defined.
if(isset($_POST["message"]) && isset($_POST["name"])) {
[...]
defined takes a string as an argument, and is for checking if a constant exists.

PHP: Notice Undefined index even using isset

i'm getting this even using 'isset':
Notice: Undefined index
it's giving the error at:
returnifisset($_COOKIE["miceusername"], ' value="', '"');
even though i am checking if the cookie isset or not. The function is:
function returnifisset($variable, $first = '', $last = ''){
if(isset($variable) and !empty($variable)){ return $first.$variable.$last; }
}
how i should modify this function to make it work and not give that error!
You are actually accessing the variable by passing it with your function, before the isset is ever called. You can't solve this problem.
You use different function names printifisset and returnifisset.
Also you can use only !empty() statement

PHP error Can't use method return > value in write context

I am getting this error in a PHP class...
Fatal error: Can't use method return
value in write context in
C:\webserver\htdocs\friendproject2\includes\classes\User.class.php
on line 35
Here is the troubled part.
if(isset($this->session->get('user_id')) && $this->session->get('user_id') != ''){
//run code
}
This code is in my contrustor, is a value is not already set for $this->session->get('user_id') then it will return false instead of a Number. So as you can see I was hoping to check if this value is a number or not or not even set.
Any help with fixing appreciated.
You can't use isset for the result of a function. Consider the following code instead:
if( $this->session->get('user_id') ){
//run code
}
isset() only works with variables as
passing anything else will result in a
parse error. For checking if constants
are set use the defined() function.
From the PHP Manual.
You can't use isset on a function. However, since false, 0, and '' all equate to a falsey statement, write your test this way:
if( $id = $this->sessions->get('user_id') ){
// Will only run if $id does not equal '', False, or 0
}
That way you have run your test and assigned the variable in one step.

Categories