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?
PHP Undefined Offset Error
Moderator: General Moderators
Re: PHP Undefined Offset Error
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.
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;
?>- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: PHP Undefined Offset Error
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] = '';
}(#10850)
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP Undefined Offset Error
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.php001 wrote:What the undefined offset means?
What value is contained in $employeeID?
Code: Select all
<?php
$employeeID = $row["employeeID"];
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP Undefined Offset Error
$row["employeeID"]; is the employeeID from the table of employee, this is the column name and also primary key.
Re: PHP Undefined Offset Error
But there is no any record in my table of employee, is that my logic error?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP Undefined Offset Error
If there is no value for $row["employeeID"] (equal to '') then it will cause the error you are receiving. A brief examplephp001 wrote:But there is no any record in my table of employee, is that my logic error?
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);
?>“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering