VickP07
12-01-2011, 05:41 AM
Hey guys so this is what I am trying to do:
I have a form where a user can add prescription data for a certain patient. The fields that the user can input data into are start_date, instructions, dose, medication name, and unit of measurement.
Right now the form has two drop down boxes being loaded with all medication names, and another drop down being loaded with units of measurements from the database.
So if the user clicks on a certain medication name and select a UOM i want to pass the ID numbers of those selection in my SQL query. My problem is this, how would i go about saving the IDs of these selections and then passing them into a variable to insert into my SQL insert statement. RIGHT NOW THE way i am passing the data the user enters, uses the value=?postdata->(then whatever the name of the field the user is inputting data into)
Right now my code will show that i am passing my start_date, dose, instructions, and patient_id. (THIS IS ALL WORKING GREAT and saving into the DB) but now i need to get the medication ID for whatever medication is selected and the UOM ID from whatever unit of measurement is selected.
prescriptions.php (where i have my form where the user inputs data and drop down boxes being loaded with data from DB):
<?php
include("config.php");
include( "office-presc.php" );
include("lock.php");
$id = $_GET['patient_id'];
// POST handler
$added = false;
if( $_POST )
{
// instantiate data class
$postdata = new User( $_POST );
if( $postdata->validate() )
{
$postdata->insert();
$added = $postdata->dose;
$postdata = NULL;
}
}
?>
<html>
<head>
<title>Add Prescription</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>Add Prescription</h2><hr />
<!--Test if user has been added, if user has been added then display corresponding message -->
<? if( $added ) { ?>
<h3><em>Prescription has been successfully added</em></h3>
<? } ?>
</td>
</tr>
<form action="prescriptions.php?patient_id=<?=$id?>" method=POST>
<table>
<td><input type="hidden" name="patient_id" id="patient_id" value="<?=$id?>">
<tr><td>Start Date:</td>
<td>Month:
<select name="month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
Day:
<select name="day">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
Year:<input type="textbox" onkeypress="return isNumberKey(event)" name="year" id="year" size="5" maxlength="4" value="<?=$postdata->year?>">
<? if( $postdata->pass_err1) { ?>
<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
<? } ?></div>
</td></tr>
<tr><td>Directions:</td>
<td><textarea rows="10" cols="30" name="instructions" id="instructions" value="<?=$postdata->instructions?>"></textarea>
<? if( $postdata->pass_err2) { ?>
<div style="color:red;" id="rolemsg2"><?=$postdata->pass_err2?>
<? } ?></div>
</td></tr>
<tr><td>Medications: </td>
<td>
<?php
$query="SELECT med_name FROM medications";
$result = mysql_query($query);
echo '<select name="medications" >';
while($row=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option value="' . $row['medication_id'] . '">' . $row['med_name'] . '</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?> </td></tr>
<tr><td>Dose:</td>
<td><input type="textbox" onkeypress="return isNumberKey(event)" name="dose" id="dose" size="3" maxlength="3" value="<?=$postdata->dose?>">
<? if( $postdata->pass_err3) { ?>
<div style="color:red;" id="rolemsg3"><?=$postdata->pass_err3?>
<? } ?> </div>
<?php
$query="SELECT name FROM UOM";
$result = mysql_query($query);
echo '<select name="UOM" >';
while($row=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option value="' . $row['unit_id'] . '">' . $row['name'] . '</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?> </td></tr>
<tr><td> </td>
<td><input type="submit" value="Submit"></td></tr>
</form>
<br />
<p></p>
<tr>
<td colspan="2" style="background-color:#FFA500;text-align:center;">
Copyright © 2011 DoctorsOfficeDB</td>
</tr>
</table>
<h4><a href="logout.php">Sign Out</a></h4>
<h4><a href="set-Rx.php">Back</a></h4>
</body>
</html>
separate php file that creates a class used to pass all form data and save into variables to be passed into SQL query (which is in the INSERT function at very bottom of code):
<?
// a class
class User
{
public $patient_id, $user_id, $dose, $month, $day, $year, $doa, $instructions;
public $pass_err1, $pass_err2, $pass_err3;
public function __construct( $post_array ) {
$this->patient_id = $_POST['patient_id'];
$this->instructions = $_POST['instructions'];
$this->dose = $_POST['dose'];
$this->month = $_POST['month'];
$this->day = $_POST['day'];
$this->year = $_POST['year'];
$this->doa = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
$this->pass_err1 = NULL;
$this->pass_err2 = NULL;
$this->pass_err3 = NULL;
}
public function validate() {
if( !$this->year)
{
$this->pass_err1 = "Please provide the Year";
}
//
if (strlen( $this->year ) < 4 )
{
$this->pass_err1 = "Year must be 4 digits long";
}
if( !$this->instructions)
{
$this->pass_err2 = "Please provide medication instructions";
}
if( !$this->dose)
{
$this->pass_err3 = "Please provide dose for medication";
}
return !$this->has_errors();
}
public function has_errors() {
return $this->pass_err1 || $this->pass_err2 || $this->pass_err3;
}
public function insert()
{
THIS IS WHERE i need to add uom_id, med_id into the insert statement
$sql = "
INSERT INTO prescriptions
(start_date, instructions, dose, pat_id)
VALUES ( '$this->doa', '$this->instructions', '$this->dose', '$this->patient_id');";
//echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
}
}
?>
I have a form where a user can add prescription data for a certain patient. The fields that the user can input data into are start_date, instructions, dose, medication name, and unit of measurement.
Right now the form has two drop down boxes being loaded with all medication names, and another drop down being loaded with units of measurements from the database.
So if the user clicks on a certain medication name and select a UOM i want to pass the ID numbers of those selection in my SQL query. My problem is this, how would i go about saving the IDs of these selections and then passing them into a variable to insert into my SQL insert statement. RIGHT NOW THE way i am passing the data the user enters, uses the value=?postdata->(then whatever the name of the field the user is inputting data into)
Right now my code will show that i am passing my start_date, dose, instructions, and patient_id. (THIS IS ALL WORKING GREAT and saving into the DB) but now i need to get the medication ID for whatever medication is selected and the UOM ID from whatever unit of measurement is selected.
prescriptions.php (where i have my form where the user inputs data and drop down boxes being loaded with data from DB):
<?php
include("config.php");
include( "office-presc.php" );
include("lock.php");
$id = $_GET['patient_id'];
// POST handler
$added = false;
if( $_POST )
{
// instantiate data class
$postdata = new User( $_POST );
if( $postdata->validate() )
{
$postdata->insert();
$added = $postdata->dose;
$postdata = NULL;
}
}
?>
<html>
<head>
<title>Add Prescription</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>Add Prescription</h2><hr />
<!--Test if user has been added, if user has been added then display corresponding message -->
<? if( $added ) { ?>
<h3><em>Prescription has been successfully added</em></h3>
<? } ?>
</td>
</tr>
<form action="prescriptions.php?patient_id=<?=$id?>" method=POST>
<table>
<td><input type="hidden" name="patient_id" id="patient_id" value="<?=$id?>">
<tr><td>Start Date:</td>
<td>Month:
<select name="month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
Day:
<select name="day">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
Year:<input type="textbox" onkeypress="return isNumberKey(event)" name="year" id="year" size="5" maxlength="4" value="<?=$postdata->year?>">
<? if( $postdata->pass_err1) { ?>
<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
<? } ?></div>
</td></tr>
<tr><td>Directions:</td>
<td><textarea rows="10" cols="30" name="instructions" id="instructions" value="<?=$postdata->instructions?>"></textarea>
<? if( $postdata->pass_err2) { ?>
<div style="color:red;" id="rolemsg2"><?=$postdata->pass_err2?>
<? } ?></div>
</td></tr>
<tr><td>Medications: </td>
<td>
<?php
$query="SELECT med_name FROM medications";
$result = mysql_query($query);
echo '<select name="medications" >';
while($row=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option value="' . $row['medication_id'] . '">' . $row['med_name'] . '</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?> </td></tr>
<tr><td>Dose:</td>
<td><input type="textbox" onkeypress="return isNumberKey(event)" name="dose" id="dose" size="3" maxlength="3" value="<?=$postdata->dose?>">
<? if( $postdata->pass_err3) { ?>
<div style="color:red;" id="rolemsg3"><?=$postdata->pass_err3?>
<? } ?> </div>
<?php
$query="SELECT name FROM UOM";
$result = mysql_query($query);
echo '<select name="UOM" >';
while($row=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option value="' . $row['unit_id'] . '">' . $row['name'] . '</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?> </td></tr>
<tr><td> </td>
<td><input type="submit" value="Submit"></td></tr>
</form>
<br />
<p></p>
<tr>
<td colspan="2" style="background-color:#FFA500;text-align:center;">
Copyright © 2011 DoctorsOfficeDB</td>
</tr>
</table>
<h4><a href="logout.php">Sign Out</a></h4>
<h4><a href="set-Rx.php">Back</a></h4>
</body>
</html>
separate php file that creates a class used to pass all form data and save into variables to be passed into SQL query (which is in the INSERT function at very bottom of code):
<?
// a class
class User
{
public $patient_id, $user_id, $dose, $month, $day, $year, $doa, $instructions;
public $pass_err1, $pass_err2, $pass_err3;
public function __construct( $post_array ) {
$this->patient_id = $_POST['patient_id'];
$this->instructions = $_POST['instructions'];
$this->dose = $_POST['dose'];
$this->month = $_POST['month'];
$this->day = $_POST['day'];
$this->year = $_POST['year'];
$this->doa = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
$this->pass_err1 = NULL;
$this->pass_err2 = NULL;
$this->pass_err3 = NULL;
}
public function validate() {
if( !$this->year)
{
$this->pass_err1 = "Please provide the Year";
}
//
if (strlen( $this->year ) < 4 )
{
$this->pass_err1 = "Year must be 4 digits long";
}
if( !$this->instructions)
{
$this->pass_err2 = "Please provide medication instructions";
}
if( !$this->dose)
{
$this->pass_err3 = "Please provide dose for medication";
}
return !$this->has_errors();
}
public function has_errors() {
return $this->pass_err1 || $this->pass_err2 || $this->pass_err3;
}
public function insert()
{
THIS IS WHERE i need to add uom_id, med_id into the insert statement
$sql = "
INSERT INTO prescriptions
(start_date, instructions, dose, pat_id)
VALUES ( '$this->doa', '$this->instructions', '$this->dose', '$this->patient_id');";
//echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );
}
}
?>