Page 1 of 1

PHP Undefined Offset Error

Posted: Wed Jul 04, 2012 8:06 pm
by php001
Undefined offset: 1 in C:\xampp\htdocs\cms\supplierIDcrement.php on line 9

line 9:
list($one[0],$one[1]) = explode('EP', $EmployeeID);

Below is the previous problems
list($one[0],$one[1]) = split('EP', $EmployeeID);

Because of the split() can't be used in the Newest version of php, so I change to explode. The problems is solved, but pop out this problems again. Who can tell what's the problems? What is the meaning of undefined offset?

Re: PHP Undefined Offset Error

Posted: Wed Jul 04, 2012 8:52 pm
by php001
Notice: Undefined offset: 1 in C:\xampp\htdocs\cms\supplierIDcrement.php on line 9
What the undefined offset means? I checked the $employeeID = $row["employeeID"]; , this is same as the column name.

Code: Select all

<?php
include("connect.php");

$query = "SELECT employeeID, number FROM employee ORDER BY number DESC LIMIT 1";
$result = $db->query($query);

$row = $result->fetch_assoc();
$employeeID = $row["employeeID"];
[color=#FF0000]list($one[0],$one[1]) = explode('EP', $employeeID);
[/color]
$index = ++$one[1];
$number = $index;

if($index < 10)
{
	$index = "EP000".$index;
}
else if($index < 100)
{
	$index = "EP00".$index;
}
else if ($index < 1000)
{
	$index = "EP0".$index;
}
else if($index < 10000)
{
	$index = "EP".$index;
}
else
{
	$index = "Your ID field is full!";
}

echo "<input type='hidden' name='EmployeeID' value='".$index."'><input type='hidden' name='number' value='".$number."'>".$index;
?>

Re: PHP Undefined Offset Error

Posted: Thu Jul 05, 2012 12:33 am
by Christopher
You are exploding the string, but it is not creating an array with two elements. The array probably only has one element. You need to do something like this to guarantee that you have two elements:

Code: Select all

$one = explode('EP', $employeeID);
if (!isset($one[1])) {
     $one[1] = '';
}

Re: PHP Undefined Offset Error

Posted: Thu Jul 05, 2012 12:35 am
by social_experiment
php001 wrote:What the undefined offset means?
I came across 'undefined offset' last night; in my case it was in an array - i tried to access $array[19] when the array only had 19 elements meaning the last element would be $array[18]. In short it is trying to access a value (in both cases in an array) that isn't defined.

What value is contained in $employeeID?

Code: Select all

<?php
 $employeeID = $row["employeeID"];
?>

Re: PHP Undefined Offset Error

Posted: Thu Jul 05, 2012 3:36 am
by php001
$row["employeeID"]; is the employeeID from the table of employee, this is the column name and also primary key.

Re: PHP Undefined Offset Error

Posted: Thu Jul 05, 2012 3:48 am
by php001
But there is no any record in my table of employee, is that my logic error?

Re: PHP Undefined Offset Error

Posted: Thu Jul 05, 2012 3:57 am
by social_experiment
php001 wrote:But there is no any record in my table of employee, is that my logic error?
If there is no value for $row["employeeID"] (equal to '') then it will cause the error you are receiving. A brief example

Code: Select all

<?php
 $name = 'FieldEPZero'; # yields -> array(2) { [0]=> string(5) "Field" [1]=> string(4) "Zero" }
 $name = 'Field'; # yields -> array(1) { [0]=> string(5) "Field" } - gives undefined offset error because there is no second element.
 $name = ''; # yields -> array(1) { [0]=> string(0) "" } - gives undefined offset error because there is no second element.
$value = explode('EP', $name);
?>