Russell Dias
Russell Dias

Reputation: 73292

Undefined Offset error PHP

Having an interesting problem with a piece of legacy web application I am working on, I keep getting the following error: Notice: Undefined offset: 0,1,2,3, etc etc

I have a jQuery tablesorter that sorts the data based on user input. This error only occurs for results that are on page 2+.

So if I go to update the results on page 1, everything is fine, I do not get the above Offset Error. However,any page other than 2 throws me that above offset error.

The following is the php code that is generating the error:

if(count($_POST)>0) {
 if ($_POST['action'] == "update"){
  // find out how many records there are to update
  $size = count($_POST['review_id']);

  // start a loop in order to update each record
  $i = 0;
  while ($i < $size) {
  // define each variable
  $approved = $_POST['approved'][$i];
  $banned = $_POST['banned'][$i];
  $review_id = $_POST['review_id'][$i];
  //var_dump($approved);
  //var_dump($banned);
  //var_dump($review_id);
  if (isset($_POST['delete'][$i])){
   $delete = 1;
  } else {
   $delete = 0; 
  }

The $approved, $banned and $review_id are the ones that cause the errors. The var_dump seems to output the string when the results are on page. however, on any pages greater than one all the var_dump outputs is NULL NULL NULL respectively.

I have checked with Firebug and all the select forms are named appropriately <select name="approved[4]" id="select2"> even the ones on page 2 <select name="approved[79]" id="select2">

Any help would be much appreciated.

EDIT: If I change the amount of rows delivered to page one it works fine. By that I mean: If I let PHP display 50 rows on the first page as opposed to the 25 currently, it updates correctly. So any amount of results on page 1 updates fine, any results that may flow over to page 2 and beyond - do not!.

Upvotes: 0

Views: 4841

Answers (1)

Stewie
Stewie

Reputation: 3121

The error means that you don't have any data at the key.

  1. Do a var dump of the POST and see if there is somewhere the variable nam is overwritten.

  2. Please check that the select statement is enclosed by <form> </form> after the page is changed. You can do the web developer toolbar > view form informaton.

Upvotes: 1

Related Questions