Insertion of form values,inserts only one fie

zhhealthcare wrote on Friday, May 14, 2010:

Hi Guys,
There is a potential bug in the working of the forms.
It occurs when we insert data.For the case of modification this issue does’t occur.
Consider the inbuilt form ‘misc_billing_options’.

We have uploaded it in the bug tracker:  http://sourceforge.net/tracker/?func=detail&atid=1245239&aid=3001651&group_id=60081
save.php of the form folder has a code as follows at line 14

foreach ($_POST as $k => $var) {
$_POST = mysql_escape_string($var);
echo “$var\n”;
}

So now the internal array pointer of the array $_POST is moved to the last position.
For details Search ‘internal array pointer’ at http://php.net/manual/en/control-structures.foreach.php

Now the same array $_POST is passed to the function ‘formSubmit’ at line 21 as below.
$newid = formSubmit(“form_misc_billing_options”, $_POST, $_GET, $userauthorized);

This function is written in the library/api.inc file at line 32 as follows
function formSubmit ($tableName, $values, $id, $authorized = “0”)

The variable $values is now the $_POST

At line 35 there is a code      while(list($key, $value) = each($values))
As the internal array pointer is at last position only the last value will be saved to the database.

Implication
The loop will be runned only once.It will not be looping the entire array elements.

Solution

Either instead of while loop use foreach.
For details see second Search result ‘internal array pointer’ at http://php.net/manual/en/control-structures.foreach.php

or add
reset($values);
to reset the file pointer before each while(if array is being manipulated as above.)
For details see search ‘26-Feb-2002 10:42’ at http://php.net/manual/en/control-structures.while.php

Basically the reason for the problem is foreach automatically initializes the pointer to first position where as while will not do that.

Thanks
Paul

stephen-smith wrote on Friday, May 14, 2010:

I have a patch against HEAD for this.  I will create a tracker item and upload ASAP.

zhhealthcare wrote on Friday, May 14, 2010:

Hey thanks.  Can you not use the  tracker already created:   http://sourceforge.net/tracker/?func=detail&atid=1245239&aid=3001651&group_id=60081  

stephen-smith wrote on Friday, May 14, 2010:

https://sourceforge.net/tracker/?func=detail&aid=3001722&group_id=60081&atid=493003 in the patch tracker.

Is this really an issue when we are not passing the array by reference?

zhhealthcare wrote on Friday, May 14, 2010:

Ignore the earlier mail.  We got the new patch, thanks.  We will try it.

Paul Simon

stephen-smith wrote on Friday, May 14, 2010:

That tracker item is your new form (I guess, the details aren’t clear), and this is not specific to your new form.  Separate issue -> separate tracker item.

zhhealthcare wrote on Friday, May 14, 2010:

My mistake:  This was the link.  https://sourceforge.net/tracker/?func=detail&aid=3001627&group_id=60081&atid=493001

Hope I got it right this time. 

stephen-smith wrote on Friday, May 14, 2010:

I can’t actually replicate your issue with the existing code.  I have sample code showing that it *is* an issue when you pass the array by reference, but we don’t pass the array by reference, AFAIK.

  • Sample Code -
    <?php

$foo = array(1,2,3);

lines($foo);
lines2($foo);
echo “foreach\n”;
foreach($foo as $bar) {
echo “$bar\n”;
}
lines2($foo);
lines($foo);

function lines($baz) {
echo “lines\n”;
while(list($qux, $quux) = each($baz)) {
echo “$quux\n”;
}
}

function lines2(&$baz) {
echo “lines2\n”;
while(list($qux, $quux) = each($baz)) {
echo “$quux\n”;
}
}
?>

bradymiller wrote on Saturday, May 15, 2010:

hey,
I also couldn’t replicate this error in the cvs version. I’m curious if the patch fixed zhhealthcare’s bug.
-brady

zhhealthcare wrote on Saturday, May 15, 2010:

Brady & Stephen

I’ll let you know as soon as Paul has tested it out.  Should be after the weekend. 
Thanks again
Sam

zhhealthcare wrote on Tuesday, May 18, 2010:

I executed the above code and it worked as desired with out any issue. In my case, only if the reset code is put the form works.I generated another form using formmaker(the form making tool coming along with openemr).They are using the code reset($field_names) just before ‘formSubmit’ function is called.This code they have put in the save.php page.The exact code is as follows
if ($_GET == “new”){
reset($field_names);
$newid = formSubmit(“form_Initial_New_Patient_Physical_Exam”, $field_names, $_GET, $userauthorized);
addForm($encounter, “Initial New Patient Physical Exam”, $newid, “Initial_New_Patient_Physical_Exam”, $pid, $userauthorized);
}elseif ($_GET == “update”) {

Paul Simon

stephen-smith wrote on Wednesday, May 26, 2010:

Just committed my patch to CVS, after a discussion on GitHub with Brady.  I will also close the tracker item(s).

stephen-smith wrote on Wednesday, May 26, 2010:

Brady, should this be applied to 3.2 as well?  If so, could you do that? I’m very rusty with my CVS and I don’t want to mess up the branching/tagging in CVS.

Either way, please update the bug and indicate if the fix was/wasn’t applied to 3.2 and then close it.

bradymiller wrote on Wednesday, May 26, 2010:

Stephen,
I’ll do this tonight. I’ll do all the 3.2 commits(just let me know if you want to put anything back in 3.2), since I can do it quickly and I also then put the new file in a patch directory (for patch releases).
thanks,
-brady

bradymiller wrote on Thursday, May 27, 2010:

hey,
Put it in the 3.2 branch and will be in next 3.2 patch.
Can test it on the 3.2 cvs demo tomorrow.
-brady