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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Insert Statement not working correctly

    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:
    Code:
    #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
    Code:
    <?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

    Code:
    <?
    // 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() );  
    
       }
    }
    
    ?>
    Last edited by VickP07; 11-21-2011 at 09:10 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Ready to kick yourself?
    Code:
    <a href="ShowPatient.php?id=<?=$row['patient_id'] ?>">
    Why would you think that you could then use
    Code:
    $_GET["patient_id"]
    ??

    Change one or the other so they match.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    VickP07 (11-21-2011)

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ready to kick yourself?
    Code:
    <a href="ShowPatient.php?id=<?=$row['patient_id'] ?>">
    Why would you think that you could then use
    Code:
    $_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 ?

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    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.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    VickP07 (11-21-2011)

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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', '');

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    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

    PHP Code:
    print_r($_GET); 
    which will show you any and all of the GET variables and values available.
    Dave .... HostMonster for all of your hosting needs

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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 ( )

  • #8
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    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).
    Dave .... HostMonster for all of your hosting needs

  • #9
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    PHP Code:
    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() );  

       }
    }

    ?> 

  • #10
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    That makes no sense at all. $_GET should be a simple array with the variable names and their values.
    Dave .... HostMonster for all of your hosting needs

  • #11
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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)

    Code:
    <?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>

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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
    Code:
         .../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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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

    Code:
    <?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
    Code:
    <?
    // 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

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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.
    Code:
    <?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>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts
    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
    Code:
    <?
    // 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() );  
    
       }
    }
    
    ?>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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