-1

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.

Sloth86x
  • 3
  • 2
  • is this the actual code you are executing ? and if so the $row[patientID] will be printed as is, try using " instead ', and wrap the patientID with citations, or just remove the $row[patientID] outside the citations and add it to the string, if it still not working, try to post the output html here so I cant check, and finally I think your doing it the hard way, you don't need a form here, you'r not posting any actual data but the id to display a single row, you can use a link instead. – Bahaa Nov 29 '20 at 15:45
  • @Bahaa thanks, using your answer and the one from Ro I managed to get it working. – Sloth86x Nov 29 '20 at 15:56
  • @Dharman thanks for the info, ill take a look at your links and use them to better my code. – Sloth86x Nov 29 '20 at 15:57

1 Answers1

0

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"
Ro Achterberg
  • 2,504
  • 2
  • 17
  • 17
  • thanks so much, this helped. I'll take a look at the links you posted so I can do better in the future! – Sloth86x Nov 29 '20 at 15:54