...

View Full Version : Saving values from form into Database



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>&nbsp;</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() );


}

}

?>

Old Pedant
12-01-2011, 06:43 AM
I'm lost.

What's special about the UOM and the medication? Why would you treat them any differently than any of the other <form> fields???

If you have something like this:


<select name="medication">
<option value="71"> Acetysalicylic Acid </option>
<option value="173"> Diltiazem ER </option>
<option value="181"> Plavix </option>
<option value="13"> Calcium Acetate </option>
</select>

Then the user sees the name of the medication, but when you use

$medid = $_POST["medication"];
you will get the value= which presumably is the ID in your medications table.

Ditto for UOM.

There's nothing special about a <select>. The same as with a checkbox or radio button, what the user sees as the value does *not* have to match the value sent by HTML to PHP.

VickP07
12-01-2011, 07:06 AM
Well the UOM and Medications drop dox boxes are special because i am loading them with a sql query on form load. They are not like the other drop boxes where i input the data myself, the med names and uom's are being loaded from the DB

VickP07
12-01-2011, 07:11 AM
As you can see i am doing a while loop to load all medication names into the drop box:


<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>


so then how exactly would i change the syntax to what you said to do(so i can still load all the names into the drop box, but just get the ID(number)):
<select name="medication">
<option value="71"> Acetysalicylic Acid </option>
<option value="173"> Diltiazem ER </option>
<option value="181"> Plavix </option>
<option value="13"> Calcium Acetate </option>
</select>

Old Pedant
12-01-2011, 09:26 AM
Looks like you are doing it right already.

Your <option> values *are* the medication_id values, so what more is there to do?

What happens when you use $_POST["medication"]??? What are you getting?

VickP07
12-01-2011, 09:36 AM
when i tried the $_POST['medication'] and echoed out my sql query it isn't picking up anything.

Old Pedant
12-01-2011, 09:42 AM
You do know that the <select> must be between the <form> and </form> tags?

Only fields between <form> and </form> will be sent by the browser.

That's the only thing I can think of off the top of my head that would be the problem.

VickP07
12-01-2011, 09:52 AM
the drop box code lines are between the <form> </form> tags

i think my problem is this: since i pass all the data that the user enters or selects from other drop down boxes (e.g. the day, month) using value="<?=$postdata->then whatever i want to call it ?> and then in the class that is where i create a variable to get that data and store it by using:
$this->year = $_POST['year']; (year being what i called the value=(whatever) in the form)

but since i am making my value for these drop down boxes and actually assigning them a value (the medication_id & med_name) i can't do a postdata->whatever i call it to get whatever i want

VickP07
12-01-2011, 05:47 PM
Okay got it working!!! i was not including the medication_id in the select statement when i created the query to load all the medications names into the drop box



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum