Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts

    update statement and display error

    Sorry guys i really didn't know what to put as a title for my problem:

    So i have a page where data for a certain patient is displayed in a table. The user can update dosage and medication name for that certain patient.

    Both update queries are working great! But, when the user clicks the update button the page reloads and the user can see that the data in the table has been updated. The dosage is doing this correctly but i having trouble figuring out how to get my medication name to display correctly on the page. AGAIN it is being UPDATED when i check my DB and if the user clicks the back button and clicks back on this certain patient he/she will see that the medication name has been changed but i want to show this right after the user clicks the update button.

    I think i need to use a while loop to maybe pass the medication name but i am not quite sure????
    In my code snippet that i am going to attach below I WILL PUT THE CODE in BOLD where i am doing the actual update and trying to pass the new medication name

    Here is my code displaying the table and where i do the UPDATE queries:
    Code:
    <?php
    include("config.php");
    include("office-update.php");
    include("lock.php");
    
    if($role != 1)
    {
    header("Location: Login.php");
    }
    
    $id = $_GET['patient_id'];
    	
    	$sql = "
    	SELECT patients.med_rec, patients.fname, patients.lname, prescriptions.start_date, medications.med_name,  prescriptions.dose, UOM.name
    	FROM prescriptions, patients, medications, UOM
    	WHERE prescriptions.pat_id = patients.patient_id
    	AND medications.medication_id = prescriptions.med_id
    	AND UOM.unit_id = prescriptions.uom_id
    	AND patients.patient_id = $id;
    	"; 
    	
    	#test if select statement works
    	$result = mysql_query( $sql );
    	$result or die("My query ($sql) failed." );	
    	
    	$row = mysql_fetch_array( $result);
    	
    // POST handler
    $added = false;	
    	
    	if( isset($_POST['dose']) && isset($_POST['medications'])  )
    	{
       // instantiate data class
       $postdata = new User( $_POST );
    
       if( $postdata->validate() )
       {    
    	   
          $added = $postdata->dose;
          $postdata = NULL;
      
    	
    	$dose = $_POST['dose'];
    	$med_id = $_POST['medications'];
    	
    	$sql5 = "
       UPDATE prescriptions
       SET dose = $dose
       WHERE prescriptions.pat_id = $id;
    	";
    
    	$sql6 = "
       UPDATE prescriptions
       SET med_id = $med_id
       WHERE prescriptions.pat_id = $id;
    	";	
    	
    	$id = $_GET['patient_id'];
    	
    	$sql = "
    	SELECT patients.med_rec, patients.fname, patients.lname, prescriptions.start_date, medications.med_name,  prescriptions.dose, UOM.name
    	FROM prescriptions, patients, medications, UOM
    	WHERE prescriptions.pat_id = patients.patient_id
    	AND  prescriptions.med_id = medications.medication_id
    	AND UOM.unit_id = prescriptions.uom_id
    	AND patients.patient_id = $id;
    	"; 
    *********************************************************	
    	$med_name = "SELECT med_name FROM medications WHERE medication_id = $med_id";
    	$name_result = mysql_query($med_name);
    
    THIS IS WHERE I WANT TO GET THE MEDICATION NAME based on the med_id that is being picked in the dropbox that contains all the medication names
    **********************************************************
    	
    	$row['dose'] = $dose; //pass new dose
    	$row['med_name'] = $name_result; //pass new medication
    ***********I KNOW $name_result is wrong but it needs to be something else but i dont know what yet. ***************
    
    	
       $result5 = mysql_query( $sql5 )
        or die( "Update failed ($sql5): " . mysql_error() );	
    	
    	$result6 = mysql_query( $sql6 )
        or die( "Update failed ($sql6): " . mysql_error() );
    
    	 }
    	
    	}			
    
    ?>
    <html>
    
    <head>
    
    <title>Patients</title>
    
    <script src="patient.js"></script>
    
    </head>
    
    <body>
    <table width="1358" border="0">
    <tr>
    <td colspan="2" style="background-color:#FFA500;">
    
    <h1>Welcome <?php echo $login_session; ?></h1>
    
    <hr /><h2>Update Prescription</h2><hr />
    
    </td>
    </tr>
    
     <!--The code below creates the table to display all the data from the DB. The headers of all the column contain a link
    	 to this same php file. The link allows the user to sort based on Medical record number, first name, or last name
    	 I used a switch statement to test for what the user clicks on to sort, and then i pass the sql query.
    -->
    
    <table border="2" CELLPADDING="2" CELLSPACING="5" width ="1000">  
    <tr>
    <th>Medical Record</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Prescription Start Date <p>(Year-Month-Day)</p></th>
    <th>Medication Name</th>
    <th>Medication Dose</th>
    <th>Unit Of Measurement</th>
    
    </tr>
    
    <p></p>
    
    <tr>
    <td><?=$row['med_rec']?></td>
    <td><?=$row['fname']?></td>
    <td><?=$row['lname']?></td>
    <td><?=$row['start_date']?></td>
    <td><?=$row['med_name']?></td>
    <td><?=$row['dose']?></td>
    <td><?=$row['name']?></td>
    </tr>
    
    </table>
    
    <form action="update-Rx2.php?patient_id=<?echo $id?>" method=POST>
    <table>
    
    <br />
    <tr><td align="right">Enter new Medication Dose: </td>
        <td><input type="text" onkeypress="return isNumberKey(event)" size="3" maxlength="3" name="dose" id="dose" value="<?=$postdata->dose?>"></td>
    		<? if( $postdata->pass_err1) { ?>
    			<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
    	<? } ?>	</div>		
    </tr>
    
    
    <tr><td>Pick New Medication: </td>
    <td>
    <?php    
    $query="SELECT medication_id, med_name FROM medications";  
    $result = mysql_query($query); ?> 
    
    <select name="medications"> 
    <?php
    while($row=mysql_fetch_array($result)){ //Array of records stored in $row  
    echo '<option value="' . $row['medication_id'] . '">' . $row['med_name'] . '</option>';  
    /* Option values are added by looping through the array */  
    }  ?>
    </select> 
    </td></tr>
    </table>
    
    <input type="submit" value="Update Dose and Medication">
    
    
    </form>
    
    </table>
    
    <h4><a href="logout.php">Sign Out</a></h4>
    
    <form method="get" ACTION="update-Rx.php">
    <input type="submit" value="Back">
    </form>
    
    </body>
    
    </html>

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    maybe something like this, but inside my php block inside the if statement???:

    while( $row = mysql_fetch_array( $result ) ) { ?>

    <tr>
    <td><?=$row['med_rec']?></td>
    <td><?=$row['fname']?></td>
    <td><?=$row['lname']?></td>
    <td><?=$row['start_date']?></td>
    <td><?=$row['med_name']?></td>
    <td><?=$row['dose']?></td>
    <td><?=$row['name']?></td>
    </tr>
    <? } ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •