Help in Resolving :: Notice: Undefined Index..... on line..

Good day sir,

I have a problem i hope you can help me solve.

I developed a program using dreamweaver and phpMyAdmin.

The program works well on my local server but when i upload to my webserver it gives the following errors

Notice: Undefined index: startyear in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 32

Notice: Undefined index: startmonth in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 32

Notice: Undefined index: startday in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 32

Notice: Undefined index: endyear in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 33

Notice: Undefined index: endmonth in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 33

Notice: Undefined index: endday in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 33

the line contains the following statements

$startdate = $_POST[‘startyear’]."-".$_POST[‘startmonth’]."-".$_POST[‘startday’];$enddate = $_POST[‘endyear’]."-".$_POST[‘endmonth’]."-".$_POST[‘endday’];

the html is

<tr valign="baseline">
<td nowrap="nowrap" align="right">Start Date:</td> 
<td>
<? //////////////////////////////////menus for selection of date 

$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'); 
print '<select name="startmonth">';

// One choice for each element in $months 

foreach ($months as $num => $month_name) 
{
print '<option value="' . $num . '">' . $month_name ."</option>n"; 
}
print "</select> n";

print '<select name="startday">';
// One choice for each day from 1 to 31
for ($i = 1; $i <= 31; $i++) 
{
print '<option value="' . $i . '">' . $i ."</option>n";
}
print "</select> n"; 

print '<select name="startyear">';// One choice for each year from last year to five years from now 
for ($year = date('Y') -1, $max_year = date('Y') + 5; $year < $max_year; $year++) 
{
print '<option value="' . $year . '">' . $year ."</option>n"; 
}
print "</select> n";
?> </td> </tr>
<tr valign="baseline"> 
<td nowrap="nowrap" align="right">End Date:</td> 
<td>
<?
//menus for selection of date 

$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'); 
print '<select name="endmonth">';
// One choice for each element in $months 

foreach ($months as $num => $month_name)
{ 
print '<option value="' . $num . '">' . $month_name ."</option>n"; 
}
print "</select> n"; 
print '<select name="endday">'; 
// One choice for each day from 1 to 31 

for ($i = 1; $i <= 31; $i++)
{ 
print '<option value="' . $i . '">' . $i ."</option>n"; 
} 
print "</select> n"; 
print '<select name="endyear">'; 
// One choice for each year from last year to five years from now 
for ($year = date('Y') -1, $max_year = date('Y') + 5; $year < $max_year; $year++) 
{ 
print '<option value="' . $year . '">' . $year ."</option>n"; 
}
print "</select> n"; ?></td> </tr>

===========================================

Please i would appreciate if you can help identify what the problem is

MOD EDIT: Added code tags

the Undefined Index is just that. You are calling an Array Variable at index XYZ and it’s not defined (has no value).

In your code you have

$startdate = $_POST[‘startyear’]."-".$_POST[‘startmonth’]."-".$_POST[‘startday’];
$enddate = $_POST[‘endyear’]."-".$_POST[‘endmonth’]."-".$_POST[‘endday’];

All the Indexes of the POST array (Presumably from a form) that are erroring have not been initialized and thus have no value. So when you try to assign them to $startdate, it throws an error or more correctly a NOTICE. (Not critical but for good code you should resolve it.)

You can fix this a couple of ways. Initialize the values before you use them or don’t call them until you know they have data.

You can CHECK the variable to see if it has data (or at least that it’s been initialized) using the isset() function http://us.php.net/isset

if (isset($_POST['startyear'])) {
   // Do something
} else {
  // Do something else
}

Thanks for a speedy reply,

How do i initialize the indexes of the POST array

i tried

$startyear = $_POST [‘startyear’];
$startmonth = $_POST[‘startmonth’];
$startday = $_POST[‘startday’];

$endyear = $_POST[‘endyear’];
$endmonth = $_POST[‘endmonth’];
$endday = $_POST[‘endday’];

$startdate = $startyear."-".$startmonth."-".$startday;
$enddate = $endyear."-".$endmonth."-".$endday;

And it didnt work.

Please send me a code with how to initialize it. So that the value would be inserted from the form.

Thanks for your help.

you could initialize it a couple of ways:

$ErrorFlag = 0; // Initialize a flag to determine if Error condition exists
if (isset($_POST ['startyear']) ) {
      $startyear = $_POST ['startyear'];
} else {
      $startyear = 1; // use a "DEFAULT" Value here.
      $ErrorFlag = 1; // In case we need to ensure we have VALID data
}

// Do the above for EACH variable you want to check.
.
.
.

if ($ErrorFlag == 1) {
    die(" Error Condition Exists in Date");
} else {
    $startyear = $_POST ['startyear'];
    $startmonth = $_POST['startmonth'];
    $startday = $_POST['startday'];

    $endyear = $_POST['endyear'];
    $endmonth = $_POST['endmonth'];
    $endday = $_POST['endday'];
}

I personally Like the Ternary Function
$AssignementVariable = CONDITION ? ValueIfConditionIsTrue : ValueIfConditionIsFalse;

$startyear = !empty($_POST ['startyear']) ? $_POST ['startyear'] :  1;

Hi again,

I tried the Ternary Function but nothing happened. Same undefined index error.

Then i tried the first option and it gave this error

Parse error: parse error, unexpected T_IF in /hsphere/local/home/trutech/thirdeyengr.com/admin/addsession.php on line 37

this is the line

$ErrorFlag = 0; // Initialize a flag to determine if Error condition exists

if (isset($_POST ['startyear']) ) {
   $startyear = $_POST ['startyear'];}
 else {
   $startyear = 1; // use a "DEFAULT" Value here.
   $ErrorFlag = 1; // In case we need to ensure we have VALID data}

if (isset($_POST ['startmonth'])) {
	 $startmonth = $_POST ['startmonth'];} else {
	$startmonth = 1; $ErrorFlag = 1;}

if (isset($_POST ['startday'])){
	$startday = $_POST ['startday'];}else {
	$startday = 1;$ErrorFlag = 1;}

if (isset($_POST ['endyear']) ) {
   $endyear = $_POST ['endyear'];}
 else {
   $endyear = 1; // use a "DEFAULT" Value here.
   $ErrorFlag = 1; // In case we need to ensure we have VALID data}

if (isset($_POST ['endmonth'])) {	
 $endmonth = $_POST ['endmonth'];}
 else {
	$endmonth = 1; 
                $ErrorFlag = 1;}

if (isset($_POST ['endday'])){
	$endday = $_POST ['endday'];}else {
	$endday = 1;
                $ErrorFlag = 1;}

$startdate = $_POST['startyear']."-".$_POST['startmonth']."-".$_POST['startday'];
$enddate = $_POST['endyear']."-".$_POST['endmonth']."-".$_POST['endday'];

What could be wrong

MOD EDIT: Added code tags

I hope my reply dint confuse you.

The 37th line is the begining of the if statement.

I think there is something wrong the with the ifelse

statement but i cant seem to figure it out.

Anybody know.

southpawnigeria :o

Part of the problem is how the code is written.

Although you CAN have multiple statements on a single line, it’s generally not recommended because it’s very easy to make errors (as is the case here).
You took my Pseudo code and tried to apply it. Then you closed and IF - ELSE statement with the curly brace at the end of a comment.

i e: ( CODE Comment CODE ERROR )
$ErrorFlag = 1; // In case we need to ensure we have VALID data }

ANYTHING after the // (on a single Line) is a Comment
INCLUDING the Closing Curly Brace so this is ACTUALLY what PHP Saw:
$ErrorFlag = 1; // In case we need to ensure we have VALID data }
When you really wanted:
$ErrorFlag = 1; // In case we need to ensure we have VALID data
}

This creates subsequent errors.

Also Laying out your code in a “Standard Format” makes it more readable and easier to troubleshoot as well.

if (isset($_POST ['startyear']) ) 
{
	$startyear = $_POST ['startyear'];
} else {
	$startyear = 1; // use a "DEFAULT" Value here.
	$ErrorFlag = 1; // In case we need to ensure we have VALID data
}


if (isset($_POST ['startmonth'])) 
{
	$startmonth = $_POST ['startmonth'];
} else {
	$startmonth = 1; 
	$ErrorFlag = 1;
}

if (isset($_POST ['startday']))
{
	$startday = $_POST ['startday'];
}else {
	$startday = 1;
 	$ErrorFlag = 1;
}

if (isset($_POST ['endyear']) ) 
{
	$endyear = $_POST ['endyear'];
} else {
	$endyear = 1; // use a "DEFAULT" Value here.
	$ErrorFlag = 1; // In case we need to ensure we have VALID data
}

if (isset($_POST ['endmonth'])) 
{
	$endmonth = $_POST ['endmonth'];
} else {
	$endmonth = 1;
	$ErrorFlag = 1;
}

if (isset($_POST ['endday']))
{
	$endday = $_POST ['endday'];
}else {
	$endday = 1;
	$ErrorFlag = 1;
}

if ($ErrorFlag == 1) {
	ECHO " THERE IS AN ERROR WITH THE DATE(S)";
} else {
	$startdate = $_POST['startyear']."-".$_POST['startmonth']."-".$_POST['startday'];
	$enddate = $_POST['endyear']."-".$_POST['endmonth']."-".$_POST['endday'];
}

Remember WHITE SPACE does NOT count against the file size or runtime of the script/application.

Sponsor our Newsletter | Privacy Policy | Terms of Service