How to solve "Notice: Undefined index: ..." - php

how can i hide the php notice by solving the issue.
my code
if(empty($_GET['mode']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
Notice: Undefined index: page in /var/www/public_html/index.php on line 1
i tried like this
if(empty($_GET['mode']) && !isset($_GET['page']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
but still showing the
Notice: Undefined index: page in /var/www/public_html/index.php on line 1
how can i solve/fix it ?

The problem is that you use in both statements !$_GET['page'] when using && operator.
When you use !$_GET['page'] in both cases even if you added !isset($_GET['page'] this condition is checked in $_GET['mode'] is empty.
You simple should probably change your statement from:
if(empty($_GET['mode']) && !$_GET['page']) {
include($basepath.'/templates/template.home.php');
}
into
if(empty($_GET['mode']) && !isset($_GET['page'])) {
include($basepath.'/templates/template.home.php');
}
In this case simple if $_GET['page'] is not set (and of course mode is empty) you should include homepage template

Either mode or page isn't present in the array $_GET - so you need to test for it using array_key_exists

Related

Notice: Undefined index with used isset and $_GET

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

PHP if $_GET isset and not equal to

I have this condition, what I am trying to do is say, if $_GET['subpage'] is not equal to registerupcoming then do this, but I get an error:
Notice: Undefined index: subpage in /Applications/XAMPP/xamppfiles/htdocs/losanihomes/views/includes/header.php on line 26
So I tried the following:
if(isset($_GET['subpage']) && $_GET['subpage'] != 'registerupcoming'){
//Do Something
}
but the problem is, if subpage is not set, then it does not go into the condition, I only want to not go into this condition if subpage is not equal to registerupcoming
Just change your && to || and isset() to !isset():
if(!isset($_GET['subpage']) || $_GET['subpage'] != 'registerupcoming'){
//Do Something
}
Now if that variable isn't set or has the wrong value you will redirect.

Notice: Undefined index: sid in C:\wamp\www\test\inc\template.php on line 205 [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
im still new to php , and i got this error .. pls help me out here , thx .
Notice: Undefined index: sid in C:\wamp\www\test\inc\template.php on line 205
so , here is my code :
<?php
}
elseif($_SESSION['sid'] != '')
{
?>
$_SESSION is an array so you set it like so $_SESSION['sid'] = 'some sid'; if you try to use $_SESSION['sid'] without setting it then PHP throws a notice.
If you want to avoid the notice then one of these options will work:
Option 1
<?php
// check if the variable isset before using it
if(isset($_SESSION['sid']) && $_SESSION['sid'] != '')
{
// do something
}
?>
Option 2
<?php
// turn off error reporting
// NEVER DO THIS IN A DEVELOPMENT ENVIRONMENT
// error_reporting(E_ALL) should be used in development
error_reporting(0);
if($_SESSION['sid'] != '')
{
// do something
}
?>
If you want to see if a session variable is set (or any variable for that matter), use isset(). If you just want to see if it is empty, use empty() after using isset()
elseif(isset($_SESSION['sid']) && !empty($_SESSION['sid']))

Undefined Index: func isset is present [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 years ago.
Receiving the following:
Notice: Undefined index: func in /Applications/XAMPP/xamppfiles/htdocs/select2/func.php on line 23
Notice: Undefined index: func in /Applications/XAMPP/xamppfiles/htdocs/select2/func.php on line 67
Notice: Undefined index: func in /Applications/XAMPP/xamppfiles/htdocs/select2/func.php on line 114
Here are the offending lines:
Line 23
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
Line 67
if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
drop_2($_GET['drop_var']);
}
function drop_2($drop_var)
{
Line 114
if($_GET['func'] == "drop_3" && isset($_GET['func'])) {
drop_3($_GET['drop_var']);
}
function drop_3($drop_var)
{
I looked in the existing questions and didn't find an answer where an if statement was attached with the operator &&.
Thanks,
Change the order of your condition to this:
if(isset($_GET['func']) && $_GET['func'] == "drop_1" )
Now you are checking if the 'func' index is set first before using it.
You have the order of your logical operators backwards. First you need to check to see if the variable is set and then you need to check it's value. If the first check is false then the second one will never happen due to short circuiting which prevents the error message you see.
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
should be:
if(isset($_GET['func']) && $_GET['func'] == "drop_1") {

Undefined index: do in

I am trying to install a arcade script and keep getting these errors, can anyone help out?
Notice: Undefined index: do in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 34
Notice: Use of undefined constant sql - assumed 'sql' in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 34
Notice: Undefined index: do in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 37
Notice: Use of undefined constant sql2 - assumed 'sql2' in C:\xampp\htdocs\meggiemoos\arcade\install\new.php on line 37
This is line 34 to 38
<? if ($_GET['do'] == sql) {
include ('sql1.php');
}
else if ($_GET['do'] == sql2) {
include ('sql2.php');
I have tried if isset($_GET but I might have done it wrong as of the closed bracket { for next line, I was told to add another bracket as well but never told where.
Thanks for your help :)
Try
if (isset($_GET['do']) && $_GET['do'] == 'sql')
sql by itself is nothing to PHP. You need to have it as a string or variable, as follows:
<? if ($_GET['do'] == "sql") {
or
$var = "sql";
<? if ($_GET['do'] == $var) {
As for the line 37 error, the server cannot find the GET variable named 'do', so you need to find out why it's not being set. You might consider adding this to earlier parts of your script:
if (!isset($_GET['do'])) {
exit("do variable was not set!");
}
else {
$do = $_GET['do'];
}
That way, you don't have to keep typing $_GET['do'], which can get tedious.

Categories