PHP calculation problem (or string interpretation problem) with data from SQL vs HTML

I’m working on a browser based scheduling application and have run into an interesting problem.

So I’ve got a schedule (staff names and their shifts, let’s say) appearing in the browser that was loaded from a database table. No problems there. The schedule data was initially created from an SQL file holding all my tables and imported directly into the server through phpMyAdmin. I click on a button in the browser to run calculations for a certain feature of the application and everything calculates beautifully.

Now, say I type over employee name Ryan D in the spreadsheet on the browser and replace it with Ryan D (yes, the exact same name; I’m trying test that the calculation succeeds again using the same data set but after a manual change has been made to the spreadsheet). I save the data to the server by clicking a save button. The save code (a PHP file) gets the data from the HTML table and creates an SQL statement that executes fine and uploads the changes to the schedule table in the database. I check phpMyAdmin after and sure enough, the data was saved: my table id numbers of the primary key have incremented but everything else (names and shifts) looks the same as it did originally…because all I did was retype Ryan D over itself.

I click on the button to run the calculations and the schedule data from the database table feed back into the code the same as before, but this time the calculations do not consider the row containing Ryan D, even though in the error_log I’m able to print out all the array values used in the calculations and see that everything is exactly the same as it was before. Super weird!

This is day 2 of dealing with the problem. I thought maybe the data was feeding in from the original SQL file differently than the data feeding from the browser via the SQL statement from the save button code (the PHP file). But that can’t be the case because I can click the save button and then rerun the calculations as many times as I want and it always works fine…as long as I don’t make any changes to the schedule.

Is there something I’m missing in how strings may be formatted behind the scenes and are being interpreted by PHP? The original schedule data that runs correctly in the calculations was imported straight from an SQL file. I even rewrote the code to load from a PHP file, and it too worked fine. So the only thing different that is happening is that changes to the schedule come from manual changes to values in an HTML table cell (td); the new values feed to a PHP file via the POST method and are then sent/saved to the database table thru an SQL query statement in that PHP file. But this doesn’t make sense to me either because the database table is getting updated with HTML table values anyway (using SQL INSERT) each time I save because I’m actually wiping the database table values clean (with SQL DELETE) beforehand.

I’m at a total loss…

On a positive note, I’ve been working on stringing together some mighty fine profanities.

Youve confused me.

How about some code !!

My apologies. Tried my best to summarize the actions of a few files and a couple thousand lines of code. But here’s the PHP snippet in question:


for ($r = 0, $rr = $nRows; $r < $rr; $r++)
{
	$counter = 0;
	for ($j = 0, $jj = $nRows; $j < $jj; $j++)
	{
		if ($counter == 0) // using $counter to make sure that the following calcs only happen once -- the first time thru when previous conditions are met
		{
			if ($checkColPrev[$r] == 1 && $checkColActive[$j] == 1)
			{
                                // NOTE: I believe the problem lies here...
				if (md5($shiftNamesPrevMonth[$r]) == md5($shiftNamesActiveMonth[$j])) // fourth attempt...fishing for anything
				//if (trim($shiftNamesPrevMonth[$r]) == trim($shiftNamesActiveMonth[$j])) // third attempt
				//if (strcasecmp($shiftNamesPrevMonth[$r], $shiftNamesActiveMonth[$j]) == 0) // second attempt
                                //if ($shiftNamesPrevMonth[$r] === $shiftNamesActiveMonth[$j]) // first attempt
				//if ($shiftNamesPrevMonth[$r] == $shiftNamesActiveMonth[$j]) // original
				{
					$counter = $counter + 1;
					for ($c = 0, $cc = $remainder; $c < $cc; $c++)
					{
						$adjHoursPrevMonth[$j][$c] = $combinedHoursPrevMonth[$r][$c]; // $adjHoursPrevMonth[$j][$c]...$j because it puts values in adj array in same position relative to $shiftHoursActiveMonth
						error_log('   (Q)$adjHoursPrevMonth[' . $j . '][' . $c . '] = ' . $adjHoursPrevMonth[$j][$c]);															
					}

                                        ... code continues ...

The code runs fine until a change is made to the schedule in the browser and that change gets loaded the to server. Running the code after this change results in the newly updated data being ignored entirely by the calculation.

In this case, the newly updated data is referenced in $checkColActive[$j] and $shiftNamesActiveMonth[$j]. Therefore, $adjHoursPrevMonth[$j][$c] no longer returns the correct values even though all the array values ( $checkColPre[$r], $checkColActive[$j], $shiftNamesPrevMonth[$r], $shiftNamesActiveMonth[$j] ) feeding in from the conditional statements above are exactly the same as they were before the change was made to the schedule.

The strings (or the names of the employees in the schedule) located in $shiftNamesPrevMonth[$r] and $shiftNamesActiveMonth[$j] no longer compare correctly.

My general question is: Is there something more to know about how strings may be formatted behind the scenes and are being interpreted by PHP depending on whether or not those strings were fed in through an HTML input or hard coded in an SQL or PHP file? Or any other suggestions?