Page 1 of 1

Table won't update

Posted: Thu Mar 06, 2008 12:52 pm
by jeff5656
My commit.php script will not update the table in the database to reflect what the user edited in the form.
There are 3 scripts and I will put them here because i'm not sure which is the one that is the problem.
This one allows the user to choose which record he wants to edit:

Code: Select all

<?php require('secure.php'); 
 
include "dateheader.php"; 
include "connectdb.php"; 
 
$query = "SELECT id_incr, patient_name, mrn, location, fellow, rcf_date, admission, consult_reason, impression, recs, comments ".
        "FROM active_consults WHERE signoff_status = 'a' ".
        "ORDER BY patient_name";
$results = mysql_query ($query) or die (mysql_error());
$num_pts = mysql_num_rows ($results);
 
$consultheading =<<<EOD
<table width="70%" border = "1" cellpadding = "2"
    cellspacing = "2" align = "center">
    
    <th> Name </th>
    <th> MRN </th>
    <th> Loc </th>
    <th> Fellow </th>
    <th> Date of Consult</th>
    <th> Reason for Admssion </th>
    <th> Reason for Consult  </th>
    <th> Impression </th>
    <th> Recs </th>
    <th> Comments </th>
    </tr>
EOD;
echo $consultheading;
 
while ($row = mysql_fetch_assoc ($results)) {
    ?>
    <tr>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['patient_name'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['mrn'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['location'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['fellow'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['rcf_date'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['admission'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['consult_reason'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['impression'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['recs'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <?php echo $row['comments'];?> </td>
    <td bgcolor="#CCCCCC" width="50%">
    <a href="editpatient.php?action=edit&id=<?php
     echo $row['id_incr']; ?>">[EDIT]</a> </td>
      </tr>
      <?php
     }
     ?>
     
<td> Total active patients: <?php echo $num_pts; ?> </td>
</tr>
 
     </table>
Then the form is populated with that record ans the patient can update any fields:

Code: Select all

<?php require('secure.php'); 
 
include "dateheader.php"; 
include "connectdb.php"; 
 
switch ($_GET['action']) {
case "edit":
    $consultsq1 = "SELECT * FROM active_consults
                        WHERE id_incr = '" . $_GET['id'] . "'";
    $result = mysql_query ($consultsq1) or die ("Invalid query: " . mysql_error ());
    $row = mysql_fetch_array ($result);
    $patient_name = $row['patient_name'];
    $mrn = $row['mrn'];
    $location = $row['location'];
    $fellow = $row['fellow'];
    $rcf_date = $row['rcf_date'];
    $admission = $row['admission'];
    $consult_reason = $row['consult_reason'];
    $impression = $row['impression'];
    $recs = $row['recs'];
    $comments = $row['comments'];
    
    break;
    
    default:
    $patient_name = ""; 
    $mrn = "";
    $location = "";
    $fellow = "";
    $rcf_date = "";
    $admission = "";
    $consult_reason = "";
    $impression = "";
    $recs = "";
    $comments = "";
    break;
    
    }
    ?>
    
    <html>
    <head>
    <title>Update Patient</title>
    </head>
    <body>
    <form action="commit.php"?action=<?php
        echo $_GET['action']; ?>&type=active_consults&id=<?php
        echo $_GET['id']; ?>" method="post">
        <table width="200" border="0">
    <tr>
      <th scope="col">Patient Name</th>
      <th scope="col">MRN</th>
      <th scope="col">Loc</th>
      <th scope="col">Fellow</th>
    </tr>
    <tr>
      <td><input name="patient_name" type="text" size="20" value="<? echo $patient_name; ?>" /></td>
      <td><input type="text" name="mrn" size="10" value="<? echo $mrn; ?>"/></td>
      <td><label>
        <input name="location" type="text" id="location" size="6" value="<? echo $loc; ?>"/>
      </label></td>
      <td><label>
        <input type="text" name="fellow" id="fellow" value="<? echo $fellow; ?>" />
      </label></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <table width="391" border="0">
    <tr>
      <th scope="col"><div align="left">Date of consult: </div></th>
      <th scope="col"><div align="left">
        <input type="text" value="<? echo $rcf_date; ?>" name="rcf_date" />
      </div></th>
    </tr>
    <tr>
      <th width="153" scope="col"><div align="left">Reason for admission:
      </div>
      <label></label></th>
      <th width="222" scope="col"><div align="left">
        <input type="text" name="admission" id="admission" value="<? echo $admission; ?>"/>
      </div></th>
    </tr>
    <tr>
      <th><div align="left">Reason for consult:
      </div>
        <label></label></th>
      <td><div align="left">
        <input type="text" name="consult_reason" id="consult_reason" value="<? echo $consult_reason; ?>"/>
      </div></td>
    </tr>
    <tr>
      <th><div align="left">Impression</div></th>
      <td><input type="text" name="impression" id="impression" value="<? echo $impression; ?>"/></td>
    </tr>
    <tr>
      <th><div align="left">Recs</div></th>
      <td><input type="text" name="recs" id="recs" value="<? echo $recs; ?>"/></td>
    </tr>
    <tr>
      <th><div align="left">Comments</div></th>
      <td><label>
        <textarea name="comments" id="comments" cols="45" rows="3" value="<? echo $comments; ?>"> </textarea>
      </label></td>
      <td><select name = "signoff_status" >
      <option value = "<?php echo $signoff_status; ?>" selected>Active
      <option value = "s">Sign off
      </select>
          </tr>
      <tr><td><input type="submit" value="Update patient"></form>
Then commit.php (below) should update the appropriate record.

Code: Select all

<?php
require('secure.php'); 
 
include "dateheader.php"; 
include "connectdb.php";
 
$sql = "UPDATE active_consults SET
    patient_name = '" . $_POST['patient_name'] . "',
    mrn = '" . $_POST['mrn'] . "',
    location = '" . $_POST['location'] . "',
    fellow = '" . $_POST['fellow'] . "',
    rcf_date = '" . $_POST['rcf_date'] . "',
    admission = '" . $_POST['admission'] . "',
    consult_reason = '" . $_POST['consult_reason'] . "',
    impression = '" . $_POST['impression'] . "',
    recs = '" . $_POST['recs'] . "',
    comments = '" . $_POST['comments'] . "',
    signoff_status = '" . $_POST['signoff_status'] . "'
     
    WHERE id_incr = '" . $_GET['id'] . "'";
    
if (isset($sql) && !empty($sql)) {
echo "<!--" . $sql . "-->";
$result = mysql_query($sql) or die ("Invalid query: " . mysql_error());
 
 
?>
<p>
Done
<?php
}
?>
But when I go back to display the records, the record is not updated.
I thank you for agreeing to look at my code and giving me a hand.

Re: Table won't update

Posted: Thu Mar 06, 2008 3:27 pm
by clrockwell79
It does not look like you are telling the script which record to update. i.e. a hidden field in the form that contains the records primary key, and then your script updates the table where id = hidden field value

Re: Table won't update

Posted: Thu Mar 06, 2008 3:45 pm
by jeff5656
I added this to the form:

Code: Select all

<td><input type="hidden" name ="id_incr" value="<?php echo $id_incr; ?>"/></td>
but it still didn't update the record.

Is this line correct?
WHERE id_incr = '" . $_GET['id'] . "'";
I also tried this without success:
WHERE id_incr = '" . $_POST['id_incr'] . "'";

and this:
WHERE id_incr = '" . $_POST['id'] . "'";

Re: Table won't update

Posted: Thu Mar 06, 2008 4:15 pm
by misteryyy
And have you tried this :

WHERE id_incr = ".$_POST['id_incr']."; ?
I think if id_incr is int, you don´t have to use slashes.