...

View Full Version : Insert Statement not working correctly



VickP07
11-21-2011, 03:42 AM
Okay guys so here is what i am doing. i have a page where i have created a table to display data from my DB. My table looks a little something like this:
|medical record number| First Name | Last Name |
------------------------------------------------
12030223 John Smith

I have made the MR numbers into links so that when the user clicks on a MR number another page will open and then the user can input appointment data for that patient he/she clicked on.

Right now when the user clicks on a patients MR number and opens the next page i am passing the "patient_id" and you can see it passes in the url
for example:
DoctorsOffice/ShowPatient.php?id=20

So here is my problem for right now i am just trying to add phone numbers for that patient the user CLICKED ON. Right now when the user inputs a phone number and cell number and clicks submit i run my sql insert statement and it is getting the phone number and cell number the user inputs on the page but when i try to pass the patient_id in the insert statement it is not picking it up and so it never saves it to the table in the DB.

My PhoneNum table in the DB contains a phone_id primary key, number, cell_num and pat_id (WHICH IS THE FOREIGN KEY) that i am using to combine it to the patients table containing the (patient_id)

So can someone please see what I am doing wrong and tell me why i can't pick up the patient_id to pass in my sql statement.

Here is the code where i create the table to allow a user to click on a Medical Record Number:


#While statement used to pass data from DB into tables

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

<br />
<tr>
<td><a href="ShowPatient.php?id=<?=$row['patient_id'] ?>"><?=$row['med_rec'] ?></a></td>
<td><?=$row['fname']?></td>
<td><?=$row['lname']?></td>
</tr>
<? } ?>

</table>


Here is the code for where the user needs to enter in a phone number and cell number (AGAIN in the URL i can see that the id is being passed but it wont pass in the SQL statement to save into the DB):
ShowPatient.php


<?php
include("config.php");
include( "office-appnt.php" );
include("lock.php");

// POST handler
$added = false;
if( $_POST )
{
// instantiate data class
$postdata = new User( $_POST );

if( $postdata->validate() )
{

$postdata->insert();

$added = $postdata->phnum;
$postdata = NULL;
}
}

?>
<html>

<head>

<title>Add Appointment</title>


</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 Appointment</h2><hr />

<!--Test if user has been added, if user has been added then display corresponding message -->
<? if( $added ) { ?>
<h3>Appointment for: <?=$added?> has been successfully added</h3>
<? } ?>

</td>
</tr>

<form action="ShowPatient.php" method=POST>

<table>

<tr><td colspan="2"><hr><h4>Patient Info:</h4></td></tr>

<tr><td>Phone Number:</td>
<td><input type="textbox" name="phnum" id="phnum" size="13" maxlength="13" value="<?=$postdata->phnum?>">
<? if( $postdata->pass_err1) { ?>
<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
<? } ?> </td></tr>

<tr><td>Cell Number:</td>
<td><input type="textbox" name="cellnum" id="cellnum" size="13" maxlength="13" value="<?=$postdata->cellnum?>">

<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-appnt.php">Back</a></h4>

</body>

</html>


This is a class file i created to store the phone num and cell num in variables and also do validation and then I do my SQL INSERT STATEMENT at the end

$id = $_GET['patient_id'] is where i am trying to get the patient id that i am passing in the url but it is not working



<?
// a class
class User
{
public $phnum, $cellnum;
public $pass_err1;


public function __construct( $post_array ) {
$this->phnum = $_POST['phnum'];
$this->cellnum = $_POST['cellnum'];
$this->role = $_POST['role'];


$this->pass_err1 = NULL;

}

public function validate() {


//user forgot to provide a number
if( !$this->phnum)
{
$this->pass_err1 = "Please provide a Phone Number";
}


return !$this->has_errors();
}

public function has_errors() {
return $this->pass_err1;
}

public function insert()
{
$id = $_GET['patient_id'];

$sql = "
INSERT INTO PhoneNum
(number, cell_num, pat_id)
VALUES ( '$this->phnum', '$this->cellnum', '$id');";

echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );

}
}

?>

Old Pedant
11-21-2011, 04:56 AM
Ready to kick yourself?


<a href="ShowPatient.php?id=<?=$row['patient_id'] ?>">


Why would you think that you could then use

$_GET["patient_id"]

??

Change one or the other so they match.

VickP07
11-21-2011, 09:57 AM
Ready to kick yourself?


<a href="ShowPatient.php?id=<?=$row['patient_id'] ?>">


Why would you think that you could then use

$_GET["patient_id"]

??

Change one or the other so they match.

I am a little confused, sorry where did I use double quotes around patient_id ?

djm0219
11-21-2011, 10:14 AM
He was trying to point out that, in your href line, you are using a GET variable named id but when you process that link you are trying to retrieve a GET variable named patient_id which, hopefully obviously, will not work.

Either change


<a href="ShowPatient.php?id=<?=$row['patient_id'] ?>">
to

<a href="ShowPatient.php?patient_id=<?=$row['patient_id'] ?>">
or change

$_GET["patient_id"]
to

$_GET["id"]

One or the other of those changes but not both.

VickP07
11-21-2011, 10:35 AM
Okay so i changed my ahref line to patient_id instead and then in the insert function
I kept the
$id = $_GET['patient_id'];

but it still won't get the patients id to pass into the sql statement:
DEBUG SQL: INSERT INTO PhoneNum (number, cell_num, pat_id) VALUES ( '(956)555-6792', '(956)555-5555', '');

djm0219
11-21-2011, 10:52 AM
When you look at the page source of the page with the href do you see the results of your change (ie: does it show the change from id to patient_id in the source)?

Right before $id = $_GET['patient_id']; in your insert function try adding


print_r($_GET);
which will show you any and all of the GET variables and values available.

VickP07
11-21-2011, 11:02 AM
yes it did change from id to patient_id=18 (a for example, after i clicked on a medical record)

and i tried the print_r and all that was displayed was this: Array ( )

djm0219
11-21-2011, 11:23 AM
Don't mean to question you but are you sure you used print_r and not just print? I would have expected a result of array( ) if you used print but not print_r (which is used to print an array).

VickP07
11-21-2011, 11:27 AM
public function insert()
{

print_r($_GET);
$id = $_GET['patient_id'];

$sql = "
INSERT INTO PhoneNum
(number, cell_num, pat_id)
VALUES ( '$this->phnum', '$this->cellnum', '$id');";

echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );

}
}

?>

djm0219
11-21-2011, 11:52 AM
That makes no sense at all. $_GET should be a simple array with the variable names and their values.

VickP07
11-21-2011, 11:55 AM
I am adding my code for the page where i first display the table containing the data from the DB i already added this above but not the whole code just the part where i create the table with the links to open the second page: (dont know if this might help)



<?php
include("config.php");
//include( "office-patient.php" );
include("lock.php");

$sql = "
SELECT patient_id, med_rec, fname, lname
FROM patients;
";

#test if select statement works
$result = mysql_query( $sql );
$result or die("My query ($sql) failed." );


?>
<html>

<head>

<title>Appointments</title>

</head>

<body>
<table width="1358" border="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">

<h1>Welcome <?php echo $login_session; ?></h1>

<hr /><h2>Patient Info</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" width ="500">
<tr>
<th><a href="set-appnt.php?sort=med_rec">Medical Record</a></th>
<th><a href="set-appnt.php?sort=fname">First Name</a></th>
<th><a href="set-appnt.php?sort=lname">Last Name</a></th>
</tr>
<h3>**You can sort the patients based on Medical Record, First Name, or Last Name**<h3>

<?
$sortswitch = $_GET['sort'];

switch ($sortswitch) {
case "med_rec":
$sql = "SELECT patient_id, med_rec, fname, lname FROM patients ORDER BY med_rec";
break;
case "fname":
$sql = "SELECT patient_id, med_rec, fname, lname FROM patients ORDER BY fname";
break;
case "lname":
$sql = "SELECT patient_id, med_rec, fname, lname FROM patients ORDER BY lname";
break;
default:
$sql = "SELECT patient_id, med_rec, fname, lname FROM patients";
break;
}

#test if select statement works
$result = mysql_query( $sql );
$result or die("My query ($sql) failed." );

#While statement used to pass data from DB into tables

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

<br />
<tr>
<td><a href="ShowPatient.php?patient_id=<?=$row['patient_id'] ?>"><?=$row['med_rec'] ?></a></td>
<td><?=$row['fname']?></td>
<td><?=$row['lname']?></td>
</tr>
<? } ?>

</table>

</table>

<h4><a href="logout.php">Sign Out</a></h4>
<h4><a href="patient.php">Back</a><h4>
</body>

</html>

Old Pedant
11-21-2011, 08:37 PM
Okay, so now let's go look at ShowPatient.php.

First of all, you are saying that when you see the URL for that page (after clicking on onw of the links from post #11) that you *DO* see the right patient id in the URL, yes?

That is, you see something like

.../ShowPatient.php?id=7182

(whatever the actual id number might be).

Yes?

Okay, then show us all of the code for ShowPatient.php. Again. Just in case we misread something.

VickP07
11-21-2011, 10:13 PM
Okay So i made some changes now.... I kept trying to think of a way to make this A LOT EASIER and finally thought about this: What if i just used a hidden input type and pass the patient_id in there that way when i post the patient_id will be saved and finally passed into the DB.

The thing is i am not sure on the syntax how to pass this variable containing the patient_id

Would i still use value="<?=$postdata->$id?>"> or just value ="<? $id ?>">

ShowPatient.php



<?php
include("config.php");
include( "office-appnt.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->phnum;
$postdata = NULL;
}
}

?>
<html>

<head>


<title>Add Appointment</title>


</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 Appointment</h2><hr />

<!--Test if user has been added, if user has been added then display corresponding message -->
<? if( $added ) { ?>
<h3>Appointment for: <?=$added?> has been successfully added</h3>
<? } ?>

</td>
</tr>

<form action="ShowPatient.php" method=POST>

<table>

<td><input type="hidden" name="patient_id" id="patient_id" value="<?=$id?>">

<tr><td>Phone Number:</td>
<td><input type="textbox" name="phnum" id="phnum" size="13" maxlength="13" value="<?=$postdata->phnum?>">
<? if( $postdata->pass_err1) { ?>
<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
<? } ?> </td></tr>

<tr><td>Cell Number:</td>
<td><input type="textbox" name="cellnum" id="cellnum" size="13" maxlength="13" value="<?=$postdata->cellnum?>">


<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-appnt.php">Back</a></h4>

</body>

</html>


office-appnt.php:
Where i create the insert function to insert all values into the table in the DB


<?
// a class
class User
{
public $phnum, $cellnum, $patient_id;
public $pass_err1;


public function __construct( $post_array ) {
$this->phnum = $_POST['phnum'];
$this->cellnum = $_POST['cellnum'];
$this->patient_id = $_POST['$id'];


$this->pass_err1 = NULL;

}

public function validate() {


//user forgot to provide a number
if( !$this->phnum)
{
$this->pass_err1 = "Please provide a Phone Number";
}


return !$this->has_errors();
}

public function has_errors() {
return $this->pass_err1;
}

public function insert()
{


$sql = "
INSERT INTO PhoneNum
(number, cell_num, pat_id)
VALUES ( '$this->phnum', '$this->cellnum', '$this->patient_id');";

echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );

}
}

?>


Oh, and yes Old Pedant after clicking on the link i am seeing in the url patient_id= some number

Old Pedant
11-21-2011, 10:40 PM
Okay, I *THINK* this does it.

I am guessing, a lot, because I can't run your code here. Plus, I don't use PHP.

But it makes sense this way. I think I am using PHP's isSet() correctly. If not, fix it.


<?php
include("config.php");
include( "office-appnt.php" );
include("lock.php");

$added = false; // assume no post data

// see if we got here via a link:
// if you only see "?id=727" in the URL then you need $_GET['id']
// if you see "?patient_id=727" in the URL, then it must be $_GET['patient_id']
// right?
//
$id = $_GET['id'];
// if we didn't get the id from the query string, then look for it in the posted form:
if ( ! isSet($id) )
{
$id = $_POST['patient_id'];
}
if ( ! isSet($id) )
{
echo "FATAL ERROR: no id from either GET or POST data!";
exit;
}

// POST handler
if( $_POST )
{
// instantiate data class
$postdata = new User( $_POST );

if( $postdata->validate() )
{
$postdata->insert();
$added = $postdata->phnum;
$postdata = NULL;
}
}

?>
<html>
<head>
<title>Add Appointment</title>
</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 Appointment</h2><hr />
<!--Test if user has been added, if user has been added then display corresponding message -->
<? if( $added ) { ?>
<h3>Appointment for: <?=$added?> has been successfully added</h3>
<? } ?>
</td>
</tr>
</table>
<?php if ( ! $added ) { ?>
<!-- this is my big addition... -->
<!-- only show form if patient was not added successfullly -->
<form action="ShowPatient.php" method=POST>
<input type="hidden" name="patient_id" id="patient_id" value="<?=$id?>">
<table>
<tr>
<td>Phone Number:</td>
<td>
<input type="textbox" name="phnum" id="phnum" size="13" maxlength="13" value="<?=$postdata->phnum?>">
<? if( $postdata->pass_err1) { ?>
<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
<? } ?>
</td>
</tr>
<tr>
<td>Cell Number:</td>
<td>
<input type="textbox" name="cellnum" id="cellnum" size="13" maxlength="13" value="<?=$postdata->cellnum?>">
</td> <!-- this was missing! -->
</tr> <!-- and so was this -->
<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>
<?php } ?>
<h4><a href="logout.php">Sign Out</a></h4>
<h4><a href="set-appnt.php">Back</a></h4>
</body>
</html>

VickP07
11-22-2011, 12:36 AM
Okay so i added the code you provided and tried entering in the phone numbers, again in the url after i clicked on the medical record link i do see ShowPatient.php?patient_id=18

But after i enter in the phone numbers and hit submit the data is added in the DB but again without the patient_id. Also clicking the submit button the page no longer shows the phone number text fields.

I don't think i am correctly passing the patient_id into my variable and thus it never sends when i try using it in my sql query:

office-appnt.php file used to create the class where the data is saved into variables and data validation, and the insert function


<?
// a class
class User
{
public $phnum, $cellnum, $patient_id;
public $pass_err1;


public function __construct( $post_array ) {
$this->phnum = $_POST['phnum'];
$this->cellnum = $_POST['cellnum'];
$this->patient_id = $_POST['$id'];


$this->pass_err1 = NULL;

}
public function validate() {


//user forgot to provide a number
if( !$this->phnum)
{
$this->pass_err1 = "Please provide a Phone Number";
}


return !$this->has_errors();
}

public function has_errors() {
return $this->pass_err1;
}

public function insert()
{

$sql = "
INSERT INTO PhoneNum
(number, cell_num, pat_id)
VALUES ( '$this->phnum', '$this->cellnum', '$this->patient_id');";

echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );

}
}

?>

Old Pedant
11-22-2011, 01:44 AM
Once again, same problem:


$this->patient_id = $_POST['$id'];

(And where in the heck did the $ come from! that's a string...you wouldn't use PHP variable inside a string inside more PHP code.)

versus this:


<input type="hidden" name="patient_id" id="patient_id" value="<?=$id?>">


That should, of course, be

$this->patient_id = $_POST['patient_id'];


THE NAMES YOU USE IN FORM FIELDS AND QUERY STRINGS MUST MATCH the $_GET[] and $_POST[] usages.

I think the best thing you could do for yourself is to get rid of *ALL* uses of 'id' and "id" and $id. Be consistent: In all places where you need a patient's id number, use patient_id as the name and $patient_id as the variable. Then you would hopefully stop tripping over things.

VickP07
11-22-2011, 02:15 AM
Yes you are right my code is very messy right now and this is causing a lot of problems for me. Thank you for the advice i will make these changes quickly to prevent further confusion.

Also, THANK YOU VERY MUCH after fixing the '$id' to 'patient_id' it works perfectly now!

Again, sorry for all the confusing messy code, but THANK you so much for your time and help!!!!

Old Pedant
11-22-2011, 03:26 AM
Not a problem. I do understand how code can get messy as you keep making little tweaks and changes here and there.

Sometimes that only solution is to go back and spend the time to clean it up. Lo and behold, it suddenly works. Because you probably fixed some little typo you hadn't even noticed.

Old Pedant
11-22-2011, 06:36 AM
That's okay...but personally I'd keep it consistent, as I said, and do

$patient_id = $_GET["patient_id"];

That is, get rid of all usage of id and $id in favor of patient_id and $patient_id.

It is *NOT* an important issue, but it will help avoid confusion in the future.

As I said, I don't use PHP. But I do use ASP and JSP and ASP.NET, and that's what I do with my code in those systems: My variable name matches the <form> field name and/or querystring name. Makes it much easier to see that the values are transferred from one to the other when the names are the same. (Actually, in ASP.NET, that's automatic: The compiler automatically uses the id of the <form> field as the variable name. Microsoft really did do something right.)

VickP07
11-22-2011, 09:17 PM
Okay so everything is working great now the data is saving correctly but i did find one "bug" or problem that can occur...

When i do my data validation on the page and then display the errors the page reloads and displays those errors, but this will get rid of the URL showing:
ShowPatient.php?patient_id=19

and it will just go to ShowPatient.php without the patient_id so then when the user enters in the data he/she forgot to input the first time and hits submit.....the patient_id will not be saved and sent to the table in theDB since it really never exists

Old Pedant
11-22-2011, 09:34 PM
Well, how do you cause the page to reload?

If you used JavaScript, you could do

location.href = "ShowPatient.php?patient_id=<?php echo $patient_id;?>";

If you do it via PHP, surely there is some equivalent way to do it.

VickP07
11-22-2011, 09:46 PM
Okay never mind i was able to figure it out, i just needed to resend my patient_id in a href line on the form.

Thanks, anyways

VickP07
11-22-2011, 10:08 PM
I do have another question now.......

Okay so as u saw in my previous code i passed the patient_id through a link on my set-appnt.php file when i created a table and then passed it in the URL
The patient_id is being saved in the variable and being used in the SQL query and that is all working great and fine, but now the table has TWO foreign keys one was the patient_id and now the other is the user_id.

Again the new code i have added is working great it is passing the patient_id (foreign key) into this new table, but i also need to pass it the user_id (second foreign key)

But, i am a little confused as to how i should go about doing this: my idea was this in my login page i could pass the user_id again in the url but my problem is this the user_id in the url would not be passed when i get to the page where i click on a link and display the patient_id in teh URL, thus the user_id would never be able to be passed into a variable and then used in my SQL query

Would you happen to have any ideas how I could go about doing this???

This is my new update page where i was getting the phone numbers like before, but now i have added two more sql queries that insert data into other tables (appointments table, and notes table) both of these tables have a pat_id (i know i know i should of kept the same names, sorry), to store the patient_id, and a u_id to store the user_id from the users table

ShowPatient.php (where i was adding the phone numbers like before now with other text fields)


<?php
include("config.php");
include( "office-appnt.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->phnum;
$postdata = NULL;
}
}

?>
<html>

<head>


<title>Add Appointment</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 Appointment</h2><hr />

<!--Test if user has been added, if user has been added then display corresponding message -->
<? if( $added ) { ?>
<h3>Appointment for: <?=$added?> has been successfully added</h3>
<? } ?>

</td>
</tr>

<form action="ShowPatient.php?patient_id=<?=$id?>" method=POST>

<table>

<td><input type="hidden" name="patient_id" id="patient_id" value="<?=$id?>">

<tr><td>Phone Number:</td>
<td><input type="textbox" name="phnum" id="phnum" size="13" maxlength="13" value="<?=$postdata->phnum?>">
<? if( $postdata->pass_err1) { ?>
<div style="color:red;" id="rolemsg1"><?=$postdata->pass_err1?>
<? } ?> </div></td></tr>

<tr><td>Cell Number:</td>
<td><input type="textbox" name="cellnum" id="cellnum" size="13" maxlength="13" value="<?=$postdata->cellnum?>">
</td> <!-- this was missing! -->
</tr> <!-- and so was this -->

<tr><td>Appointment Time:</td>
<td>Hour:
<select name="hour">
<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>
Minutes:
<select name="min">
<option value="00">00</option>
<option value="05">05</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
</select>
<select name="ampm">
<option value="PM">PM</option>
<option value="AM">AM</option>
</select>
</td></tr>

<tr><td>Appointment 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_err2) { ?>
<div style="color:red;" id="rolemsg2"><?=$postdata->pass_err2?>
<? } ?></div>
</td></tr>

<tr><td>Appointment Description:</td>
<td><textarea rows="10" cols="30" name="desc" id="desc" value="<?=$postdata->desc?>"></textarea>
<? if( $postdata->pass_err3) { ?>
<div style="color:red;" id="rolemsg3"><?=$postdata->pass_err3?>
<? } ?></div>
</td></tr>

<tr><td>Additional Notes:</td>
<td><textarea rows="10" cols="30" name="notes" id="notes" value="<?=$postdata->notes?>"></textarea>
<? if( $postdata->pass_err4) { ?>
<div style="color:red;" id="rolemsg4"><?=$postdata->pass_err4?>
<? } ?></div>
</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-appnt.php">Back</a></h4>

</body>

</html>


office-appnt.php: (where i create the class to store all the input into variables and then create teh sql queries in the INSERT FUNCTION


<?
// a class
class User
{
public $phnum, $cellnum, $patient_id, $hour, $min, $ampm, $time, $month, $day, $year, $doa, $desc, $notes;
public $pass_err1, $pass_err2, $pass_err3, $pass_err4;


public function __construct( $post_array ) {
$this->phnum = $_POST['phnum'];
$this->cellnum = $_POST['cellnum'];
$this->patient_id = $_POST['patient_id'];
$this->desc = $_POST['desc'];
$this->hour = $_POST['hour'];
$this->min = $_POST['min'];
$this->ampm = $_POST['ampm'];
$this->time = $_POST['hour'] . ":" . $_POST['min'] . " " . $_POST['ampm'];
$this->month = $_POST['month'];
$this->day = $_POST['day'];
$this->year = $_POST['year'];
$this->doa = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
$this->notes = $_POST['notes'];


$this->pass_err1 = NULL;
$this->pass_err2 = NULL;
$this->pass_err3 = NULL;
$this->pass_err4 = NULL;

}
public function validate() {


//user forgot to provide a number
if( !$this->phnum)
{
$this->pass_err1 = "Please provide a Phone Number";
}

if( !$this->year)
{
$this->pass_err2 = "Please provide Appointment Year";
}

if( !$this->desc)
{
$this->pass_err3 = "Please provide Appointment Description";
}

if( !$this->notes)
{
$this->pass_err4 = "Please provide Office visit note";
}


return !$this->has_errors();
}

public function has_errors() {
return $this->pass_err1 || $this->pass_err2 || $this->pass_err3 || $this->pass_err4;
}

public function insert()
{

$sql = "
INSERT INTO PhoneNum
(number, cell_num, pat_id)
VALUES ( '$this->phnum', '$this->cellnum', '$this->patient_id');";

//echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die( "Error( $sql): " . mysql_error() );

//in this next sql query the insert statement should have
//(date, time, descr, pat_id, u_id) but since i haven't figured out how to pass the user_id that is logged into the system i haven't added it yet)

$sql2 = "
INSERT INTO appointments
(date, time, descr, pat_id)
VALUES ('$this->doa', '$this->time', '$this->desc', '$this->patient_id');";

mysql_query($sql2) or die ("Error( $sql2 ): " . mysql_error() );

$sql3 = "
INSERT INTO notes
(visit_note, pat_id)
VALUES ('$this->notes', '$this->patient_id');";

mysql_query($sql3) or die ("Error( $sql3 ): " . mysql_error() );

}

}

?>

VickP07
11-22-2011, 10:11 PM
Login.php:
inside of the if ($count == 1) statement is where i thought i could pass the user_id
some how in the URL


<?php
include("config.php");

//start session
session_start();

if($_POST)
{
// username and password sent from Form
$myusername = $_POST['username'];
$mypassword = $_POST['password'];

$sql="SELECT user_id FROM users WHERE username='$myusername' and pword=aes_encrypt('The Secret Phrase', '$mypassword');";


$result = mysql_query($sql);

$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if ($count == 1)
{
$_SESSION['login_user']=$myusername;

header("Location: add-user-office.php");
exit();
}
else //wrong password or username
{
$error="Your Login Name or Password is invalid";
}
}
?>
<html>

<head>

<title>DoctorsOfficeDB</title>

</head>
<body>

<table width="1358" border="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">

<hr /><h1 align="center">Doctor's Office DB</h1><hr /></td></tr> </table>

<form action="Login.php" method="post">
<fieldset style="text-align:center;">

<legend><h3>Login Information:</h3></legend>
UserName: <input type="text" size="20" name="username" /><br />
Password: <input type="password" size="20" name="password" /><br />
<input type="submit" value="Submit">
</fieldset>
</form>

<div style="font-size:13px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

<br />
<p>The current date and time is: </h4></p> <code> <?php print strftime('%a') ?> <?php print strftime('%b') ?> <?php print strftime('%e') ?> <?php print strftime('%r') ?> <?php print strftime('%G')?> </code>
</body>
</html>

Old Pedant
11-22-2011, 10:31 PM
Not to ask a dumb question, but...

When you do this:

$_SESSION['login_user']=$myusername;

why not simply *ALSO* do

$_SESSION['user_id'] = $row["user_id"];

Now it's available any place in any code where you need it.

What am I missing??

VickP07
11-22-2011, 10:42 PM
Why the double quotes on the second "user_id" shouldn't it be single quotes?

VickP07
11-22-2011, 10:48 PM
So then in my set-appnt.php where i make the sql queries it would be something like this:


$sql2 = "
INSERT INTO appointments
(date, time, descr, pat_id, u_id)
VALUES ('$this->doa', '$this->time', '$this->desc', '$this->patient_id', $_SESSION['user_id']);";

mysql_query($sql2) or die ("Error( $sql2 ): " . mysql_error() );

Old Pedant
11-23-2011, 12:20 AM
Looks right to me.

When in doubt, DEBUG DEBUG DEBUG.

In this case, simply do

echo "<hr>DEBUG SQL: " . $sql . "<hr/>\n";

Old Pedant
11-23-2011, 12:24 AM
Why the double quotes on the second "user_id" shouldn't it be single quotes?

Makes no difference whatsoever in this kind of string, where there are no special considerations.

The only times in PHP you *must* use double quotes are when you want to include PHP variables and/or escape sequences in the string.

Read this:
http://www.php.net/manual/en/language.types.string.php


Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

And also see the examples there.

And I don't even use PHP and knew this. Maybe time to take a small refresher course in PHP? Maybe just read through the online manual one time, at least? Learning by example will only go as far as the examples go.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum