Skip to content
Advertisement

PHP update record button not passing information to another page

im trying to make a update record button on php that when clicked, it’ll take you to another page showing only the information of the patient which update button was pressed. Im using the following to show the patient information along with an update button for each patient

patients.php

<?php
        require_once 'includes/dbh.inc.php';

        $result = mysqli_query($conn,"SELECT * FROM patientProfile ORDER BY patientLastName ASC");
        echo "<table class=table table-striped table-sm>
        <tr>

        <th>Last Name</th>
        <th>Name</th>
        <th>Gender</th>
        <th>Age</th>
        <th>Medical Plan</th>
        <th>Record Number</th>
        <th></th>
        </tr>";

        while($row = mysqli_fetch_array($result))
        {
          echo "<tr>";

          echo "<td>" . $row['patientLastName'] . "</td>";
          echo "<td>" . $row['patientName'] . "</td>";
          echo "<td>" . $row['patientGender'] . "</td>";
          echo "<td>" . $row['patientAge'] . "</td>";
          echo "<td>" . $row['medicalPlan'] . "</td>";
          echo "<td>" . $row['patientID'] . "</td>";
          echo "<td>" . '<form method="POST" action="patientEdit.php"><input type="hidden" name="pid" value="$row[patientID]"><input type="submit" name="submit_btn" value="Update"></form>' . "</td>";
          echo "</tr>";
        }
        echo "</table>";

        mysqli_close($conn);
        ?>

When I click the update button I want it to take me to another page, which has the following code

patientEdit.php

        <?php

        require_once 'includes/dbh.inc.php';
        if (isset($_POST["submit_btn"])) {
          $patient = $_POST["pid"];
        $result = mysqli_query($conn,"SELECT * FROM patientProfile WHERE patientID = '$patient' ");
        echo "<table class=table table-striped table-sm>
        <tr>
        <th>Record Number</th>
        <th>Last Name</th>
        <th>Name</th>
        <th>Gender</th>
        <th>Age</th>
        <th>Email</th>
        </tr>";

        while($row = mysqli_fetch_array($result))
        {
          echo "<tr>";
          echo "<td>" . $row['patientID'] . "</td>";
          echo "<td>" . $row['patientLastName'] . "</td>";
          echo "<td>" . $row['patientName'] . "</td>";
          echo "<td>" . $row['patientGender'] . "</td>";
          echo "<td>" . $row['patientAge'] . "</td>";
          echo "<td>" . '<input type="text" name="changeEmail" value="$row[email]">' . "</td>";
          echo "</tr>";
        }
        echo "</table>";
      }

        mysqli_close($conn);
        ?>

What I want to happen on this pages is to show the information of only the patient that was on the row where I clicked the update button, I also have a textbox where I would like to display the current email on the database so they can change it if necessary and not sure if that is the correct way to do it.

Right now, the page only shows the table headers with no info, I tried putting “” on patientID as shown below but no result.

<input type="hidden" name="pid" value="$row["patientID"]">

Also tried changing the query on patientEdit.php to

$result = mysqli_query($conn,"SELECT * FROM patientProfile WHERE patientID LIKE '%$patient%'

Also, nothing happened.

Any help would be appreciated, if more info is needed ill be glad to provide it.

Advertisement

Answer

Change this:

echo "<td>" . '<form method="POST" action="patientEdit.php"><input type="hidden" name="pid" value="$row[patientID]"><input type="submit" name="submit_btn" value="Update"></form>' . "</td>";

To this:

echo "<td>" . '<form method="POST" action="patientEdit.php"><input type="hidden" name="pid" value="' . $row['patientID'] . '"><input type="submit" name="submit_btn" value="Update"></form>' . "</td>";

However, if your ‘Update’ button is only there to navigate to the edit form, it should probably just be a link (<a href="patientEdit.php?pid=YOUR_PATIENT_ID">). You then read out $_GET['pid'] in patientEdit.php instead. See also When do you use POST and when do you use GET?.

The reason behind your initial error is that variables will not be expanded inside single quoted strings. Consider the following to demonstrate:

<?php

$row = ['patientID' => 123];

var_dump('$row[patientID]'); // string(15) "$row[patientID]"
var_dump('{$row[patientID]}'); // string(17) "{$row[patientID]}"
var_dump("$row[patientID]"); // string(3) "123"
var_dump("{$row['patientID']}"); // string(3) "123"
var_dump("{$row[patientID]}"); // Fatal error: Uncaught Error: Undefined constant "patientID"
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement