1

This code was written 10-15 years ago from what I can see. Friday, the webhost stopped supporting php4 and forced upgrade to 5.6.23 and the web app stopped working. I have been going over it all weekend. Got the system running, but there is one serious issue and I've narrowed it to a specific line of code and still can't figure it out due to my not doing php OOP programming myself. Looking for a quick fix here as my friend is dead in the water.

This code:

$ins = & $_SESSION["ins"];
$serv = new Services();

print "post - ";
print_r($_POST)."<br />";
reset($_POST);
foreach ($_POST as $key => $value) {
    //$key = addslashes($key);
    $query = mysql_query("SELECT $valueNumber
                          FROM CLIENTS_SERVICES
                          where serviceID = '$key' and clientID = '$ins->client' and effectiveDate <= '$effectiveDate'
                          order by effectiveDate DESC LIMIT 1")
             or die("SELECT services function query failed!!");
    $query_data = mysql_fetch_object($query);
    $serv->value = $query_data->$valueNumber;
    $serv->quantity = $value;
    $ins->services[$key] = $serv;    //<-----  this is the offender

    // Calculate services running total
    $total = $total + ($serv->quantity * $serv->value);
    print $key." - key<br />";
    print $value." - value<br />";
    print "serv array - ";
    print_r($serv);
    print "<br />";
    print "ins array - ";
    print_r($ins->services);
    print "<br />";
    print $total." - total<br />";
}

outputs:

post - Array (
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] => 1.5
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
[15] =>
[16] => 3
[17] =>
[50] =>
[51] => 1.75
[58] =>
[save_services] => Save )

6 - key
1.5 - value
serv object - Services Object ( [quantity] => 1.5 [value] => 56 )
ins array - Array ( [6] => Services Object ( [quantity] => 1.5 [value] => 56 ) )
84 - total

16 - key
3 - value
serv object - Services Object ( [quantity] => 3 [value] => 45 )
ins array - Array ( [6] => Services Object ( [quantity] => 3 [value] => 45 ) [16] => Services Object ( [quantity] => 3 [value] => 45 ) )
219 - total

51 - key
1.75 - value
serv object - Services Object ( [quantity] => 1.75 [value] => 118 )
ins array - Array ( [6] => Services Object ( [quantity] => 1.75 [value] => 118 ) [16] => Services Object ( [quantity] => 1.75 [value] => 118 ) [51] => Services Object ( [quantity] => 1.75 [value] => 118 ) )
425.5 - total

In a nutshell, I cannot understand why the most recent object values are overwriting the previous array entries. I've spent the weekend trying to figure out what changed between the old version (4.?) and the new 5.6.23, and have been on dozens of dead ends and wild goose chases. Ignorance is not bliss.

If there is a pointer to the resolution, or you can offer some insight, I'd appreciate it.

Owen Parker
  • 253
  • 2
  • 12
  • 1
    Well for starters, [mysql_* functions are long deprecated](http://stackoverflow.com/q/12859942/1255289), unmaintained, and insecure. Get rid of them! – miken32 Mar 20 '17 at 18:02
  • 1
    Also you don't show where `$serv` and `$ins` come from but: http://php.net/manual/en/language.oop5.references.php – AbraCadaver Mar 20 '17 at 18:05
  • PHP Migration's page: https://secure.php.net/manual/en/migration5.php – Gabriel Heming Mar 20 '17 at 18:11
  • @AbraCadaver - added the source of the class/array and object I have pored over the migration and changes to OOPs docs numerous times. I feel that the issue MUST lie in the "objects are passed by references by default" change, but I am just too ignorant of classes and such to see what's up. To me, it comes down to adding an object to the class/array variable and it not behaving as one would expect. The key is not being overwritten, just the object values. – Owen Parker Mar 20 '17 at 18:25
  • @miken32 - very much agree, but not part of the issue at hand – Owen Parker Mar 20 '17 at 18:25

1 Answers1

1

Create a new instance of services inside of the loop

foreach ($_POST as $key => $value) {
    $serv = new Services();
    ...
}

When you use objects in php it will use the same pointer to that object

meda
  • 45,103
  • 14
  • 92
  • 122
  • that's it and I knew it would be when I saw it. This system worked the other way for nearly 15 years. That's the tricky part. I'm terribly grateful for this seemingly simple answer meda. – Owen Parker Mar 20 '17 at 18:37