Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Summarizing the main problem at the top here to save people time: When the form is submitted to input all of the test results, it doesn't record all of the necessary variables into the database, and leaves those values blank instead. There may also be some incorrect usage of arrays.

Okay guys, I've a system set up for a doctor to record a selection of test results for patients on a specific type of medication. For the project it is to only cover one set of results, but there are bonus marks for showing how it can easily expand to include the tests for other medications.

What I've done is set up a form with:

  1. a series of options to record the details across all of the tests recorded on that day (eg. the brand of medicine being used, the dosage level).
  2. within the same form, a loop to generate all of the tests which are to be tested for that medication in a table, with a text input field named for entering the value into an array for results and another array recording the names of any tests where a value has been entered.
  3. an if statement which, upon submitting the form, is to work through both arrays, inserting all of the results into the database.

Declaration of arrays and form loop

$listTests=mysql_query($findTests);
        if($listTests){ 
        $result=array();
        $testnames=array();
?>  
        <table><tr><th>Test Name</th><th>Most Recent Result</th><th>New Result</th></tr>
<?php           while($testList = mysql_fetch_array($listTests)){
                echo("<tr><td>{$testList[0]}</td><td>{$testList[7]}</td><td><input type='text' name='testresult[]'></td></tr>");
                $testnames[] = $testList[0];
            }
            mysql_free_result($listTests); ?>
</table>
<?php   }
        else{
            echo "An error occurred ".mysql_error();}

and the loop after the form has been submitted is as follows:

if(isset($_POST['record'])){
            $findRecordID = "SELECT MAX(RecordID) FROM RecordsDetail WHERE PatientID='{$_SESSION['patient']}'";
            $RecordID = mysql_query($findRecordID);
            list($record_id) = mysql_fetch_array($RecordID);
            for($i=0; $i<count($_POST['testresult']); $i++){
                $result[$i] = $_POST['testresult'][$i];
            }
            for($j=0; $j<count($result); $j++){
                if($result[$j]!=""){
                $InsertResult = "INSERT INTO TestRecords (RecordID, TestName, Medication, Result) 
                VALUES({$record_id}, '{$testnames[$j]}', '{$medication}', {$result[$j]})"; //testname and medication show undefined offset errors
                $sqlInsert = mysql_query($InsertResult) or die(mysql_error());
                }
            }
        }

The database receives the correct Record ID value and Test Result value for the first entry on the list each time, and the other part of the form for recording the details of the visit works fine, but for whatever reason it doesn't pick up the Name of the test or the medication. I'm not wholly sure why because the medication is a constant throughout the page and is picked up fine in the other record, while the Test Name prints out fine onto the page, I can't be as confident about it as it's within the loop. The loop does attempt to record the second result, but as it has the same record number and is recording blank entries for test name and medication, it's throwing up an error for using the same primary key (all three combined function as a primary key).

The errors which currently appear are as follows:

Notice: Undefined offset: 0 in ****** on line 82
Notice: Undefined offset: 1 in ****** on line 82
Duplicate entry '97--' for key 'PRIMARY'

Line 82 is the $sqlInsert variable. The value 97 was the Record ID of that entry.

Apologies for the messy code, I tried to tidy it up as much as possible, but I'm on the verges of giving up and didn't want to tweak anything away from it's current state too much. Mostly just curious about what I'm doing wrong in regards to it not inserting the test name and medication variables.

Thank you for your time!

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.