PHP Notice: Undefined index in the error log.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srirams
Forum Commoner
Posts: 36
Joined: Wed May 25, 2005 8:57 am
Location: India
Contact:

PHP Notice: Undefined index in the error log.

Post by srirams »

Hi,

I have a calendar page which actually loads fine.
The calendar code starts like this.

Code: Select all

<?php 
require_once("include/inc_calendar.class.php"); 
$oCalendar = new inc_calendar(); 
$month = $_REQUEST['month']; 
$year = $_REQUEST['year']; 
$oCalendar->dateNow($month,$year);
?>
Though the calendar loads without any errors/warnings each time I navigate to this page but the errorlog logs the below notice.
I believe that the notice is indicating that the index value month is not set.

Code: Select all

[Sat May 03 14:29:50 2008] [error] [client 127.0.0.1] PHP Notice:  Undefined index:  year in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\mysite\\view_calendar.php on line 5, referer: http://localhost:8080/mysite/menu.php 
 
[Sat May 03 14:29:50 2008] [error] [client 127.0.0.1] PHP Notice:  Undefined index:  month in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\mysite\\view_calendar.php on line 5, referer: http://localhost:8080/mysite/menu.php
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: PHP Notice: Undefined index in the error log.

Post by yacahuma »

do a print_r on the request. at some point you are not sending the index year and month
User avatar
Kastor
Forum Newbie
Posts: 24
Joined: Thu May 01, 2008 2:29 am
Location: Grodno, Belarus
Contact:

Re: PHP Notice: Undefined index in the error log.

Post by Kastor »

Yes, it is most likely that the index value month is not set and $oCalendar->dateNow($month,$year) takes $month as null or 0

Code: Select all

<?php
  echo “<pre>”
  print_r($_REQUEST);
  // or
  if (!isset($_REQUEST[‘month’])) die(‘Month is not set’);
?>
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: PHP Notice: Undefined index in the error log.

Post by lafever »

That's because you're using undefined variables in the code. The $_REQUEST is not set so the values of $month and $year are null. You could do something simply like so.

Code: Select all

 
$month = isset($_REQUEST['month']) ? (int)$_REQUEST['month'] : date('m');
$year   = isset($_REQUEST['year']) ? (int)$_REQUEST['year'] : date('Y');
 
srirams
Forum Commoner
Posts: 36
Joined: Wed May 25, 2005 8:57 am
Location: India
Contact:

Re: PHP Notice: Undefined index in the error log.

Post by srirams »

Hi,

Thanks all for the responses.
Lafever's below code helped. Good sharp thinking mate. Much appreciated.
Post Reply