Link to home
Start Free TrialLog in
Avatar of UniqueData
UniqueDataFlag for United States of America

asked on

undefined index

I am getting the error:  Notice: Undefined index: OTHours in /addActivities.php on line 24

Line 24 is the if statement at the beginning of:
    	if ($entry['RegHours']+$entry['OTHours']+$entry['Units'] > 0 ) {
			$sql="Insert Into StaffActivity (StaffID, ActivityID, RegHours, OTHours, Units, ActivityDate) 
					values ('".$entry['StaffID']."',".$entry['ActivityID'].",".
							$entry['RegHours'].",".$entry['OTHours'].",".
							$entry['Units'].",'".$_POST['activityDate']."')";
			$outputResult=$sql;
	    	if (!$res = mysql_query($sql)) die( mysql_error() );
    	}

Open in new window


however, the insert is working perfectly and I count the number of OTHours in the document and it matches the count of RegHours and Units.  In fact, the form is built dynamically and there is always an OTHours for every RegHours.

Any idea what could be happening?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

I suspect that code is within another loop (something like in the other question). Usually, this kind of errors is because php try to loop one more time over the total amount of rows: can you show the loop, if there is one?
Let's go with this as being line 24:

if ($entry['RegHours']+$entry['OTHours']+$entry['Units'] > 0 ) {

Just before that statement, put in this line:

var_dump($entry);

Then you can see what keys and data are present inside the $entry array
Avatar of UniqueData

ASKER

interesting.  The var_dump stops at:
["5f79aa69a56bf82ec8dbf4fddb94062058"]=>
    array(5) {
      ["StaffID"]=>
      string(2) "71"
      ["ActivityID"]=>
      string(2) "58"
      ["Units"]=>
      string(0) ""
      ["RegHours"]=>
      string(0) ""
      ["OTHours"]=>
      string(0) ""
    }
    ["5f79aa69a56bf82ec8dbf4fddb94062059"]=>
    array(4) {
      ["StaffID"]=>
      string(2) "71"
      ["ActivityID"]=>
      string(2) "59"
      ["Units"]=>
      string(0) ""
      ["RegHours"]=>
      string(0) ""
    }
  }

Open in new window


notice that ActivityID 58 has all 5 elements while ActivityID only has 4 (it is missing the OT).
However, when I look at the source for the page I see:
<td>
	<input type='hidden' name='entry[5f79aa69a56bf82ec8dbf4fddb94062058][StaffID]' value='71'>
	<input type='hidden' name='entry[5f79aa69a56bf82ec8dbf4fddb94062058][ActivityID]' value='58'>
	<input class='unitVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062058][Units]' size='4'>
</td>
<td>
	<input class='regVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062058][RegHours]' size='4'>
</td>
<td>
	<input class = 'otVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062058][OTHours]' size='4'>
</td>
<td>
	<input type='hidden' name='entry[5f79aa69a56bf82ec8dbf4fddb94062059][StaffID]' value='71'>
	<input type='hidden' name='entry[5f79aa69a56bf82ec8dbf4fddb94062059][ActivityID]' value='59'>
	<input class='unitVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062059][Units]' size='4'>
</td>
<td>
	<input class='regVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062059][RegHours]' size='4'>
</td>
<td>
	<input class = 'otVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062059][OTHours]' size='4'>
</td>
<td>
	<input type='hidden' name='entry[5f79aa69a56bf82ec8dbf4fddb94062060][StaffID]' value='71'>
	<input type='hidden' name='entry[5f79aa69a56bf82ec8dbf4fddb94062060][ActivityID]' value='60'>
	<input class='unitVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062060][Units]' size='4'>
</td>
<td>
<input class='regVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062060][RegHours]' size='4'>
</td>
<td>
<input class = 'otVal' type='text' name='entry[5f79aa69a56bf82ec8dbf4fddb94062060][OTHours]' size='4'>
</td>

Open in new window

and so on.  But no entries after this Activity 59 are getting submitted.  I don't see anything wrong with the html  (note, I added the carriage returns in the html to make it readable.  My loops just put it all together in one line.

Here is my looping code:
		          <tbody>
          		<?php 
          			while($resUsers = mysql_fetch_array($rstUsers)) {
          				echo "<tr>";
          				echo "<td class='nameColumn'>".$resUsers['LastName'].', '.$resUsers['FirstName']."</td>";
						mysql_data_seek($rstActivities, 0) ;      					
  						while($row = mysql_fetch_assoc($rstActivities))
      					{
							echo "<td>";
								// $resUsers['uuk'].$row['ActivityID'] creates a unique id instead of keeping a loop counter
								echo "<input type='hidden' name='entry[".$resUsers['uuk'].$row['ActivityID']."][StaffID]' value='".$resUsers['_key']."'>";
								echo "<input type='hidden' name='entry[".$resUsers['uuk'].$row['ActivityID']."][ActivityID]' value='".$row['ActivityID']."'>";
								echo "<input class='unitVal' type='text' name='entry[".$resUsers['uuk'].$row['ActivityID']."][Units]' size='4'>";
							echo "</td>";
							echo "<td><input class='regVal' type='text' name='entry[".$resUsers['uuk'].$row['ActivityID']."][RegHours]' size='4'></td>";
							echo "<td><input class = 'otVal' type='text' name='entry[".$resUsers['uuk'].$row['ActivityID']."][OTHours]' size='4'></td>";						
						}      					

					echo "</tr>";
					}
				?>
	            </tbody>  

Open in new window

Well, it "smells" like a logic error, and I don't think I can debug that based on the code - we would need to see all the data and test cases, too.  Something is going missing, and with inputs of type=text, there should always be a field present - perhaps empty, but always a defined index.  Perhaps step through it a line at a time and check to see where the OTHours index is used (case-sensitive).

In semi-related news, you probably want to be aware of this:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
I know how to step through vba code, but how do you step through php/html?
I excluded that one staff record and now the next staff ID has the issue.  
I then excluded the Activity it was hanging up on and it still didn't work.
It is always at the same point:

array(2) {
  ["activityDate"]=>
  string(10) "2015-03-11"
  ["entry"]=>
  array(200) {
    ["0b840da71056d6fe56d78bf6f8f9169c13"]=>
    array(5) {

Open in new window


am I hitting some max of an array and it can't create any more?  It always stops at array(200) with the final array of 4. I  googled array limits and it seems to be a huge number.
You're not hitting an array limit.  There is probably something in the program logic that is creating a limit.  Here is how I would try to deconstruct the problem.

First, find the query that generates the $rstUsers results set.  Pull this out into its own script.  You will want to remove all of the other elements and just run this query so you can understand the results.  You can run the query, then retrieve the results set and inspect all of the rows with var_dump().  There may be some obvious clues (or not) but at least you will be able to prove to yourself that it's not the query that is the cause of the problem.

After that, begin to rebuild the application a piece at a time using the query results set.  As you put the parts back together you can test each step, step-by-step, to see where the logic error intrudes.  It's a lengthy process but it's the price we pay to become successful developers.  If you choose to turn toward Test Driven Development, you'll start getting faster results almost immediately!  Highly recommended.
SOLUTION
Avatar of UniqueData
UniqueData
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You should ask your hosting company, for sure.  They may be using "Suhosin" or some similar software add-on that can cripple PHP's abilities (they call this "security," but it's just a nuisance).

You may want to run phpinfo() and scan the output for the string "max" since there are some PHP limits that can be in play.  You may also want to get a professional programmer to take a look at the application.  If you're hitting the maximum input limits there is something wrong in the overall design.  My general recommendation (without having studied your specific situation) would be to put those large data collections into a database, and send just the database keys that would be needed to recover the data.
Since I don't know how many people are going to be in the staff table, and even the activity record count could go up eventually, I should probably come up with a way that can handle more array items without editing the ini file.  I will research stringify or other options for sending the data to a php file.

One thing, they will not be entering activities for every staff for every activity, so I am thinking I need to loop through anything that has a qty, regHours, or otHours and only send those records.  I do that in the post code already, but that is after everything tries to get shoved into the POST :)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The initial issue was that I was hitting the upper limit of the array.  The work-around was provided in my other question.