Hello!

I've got the code below which takes data from my db and displays it to the page listing reports submitted by the user. At the end of each row I have button that is supposed to launch a PDF generator so that the user can view their report. I have multiple rows, yet only the bottom row button will work correctly. If I click the submit button on any of the rows prior to the last row, the data for the last report will be displayed. I'd appreciate any help with this. Thanks!

<form name="vin_report_table" action="reportPDF.php" method="post">
        <table>
<?php
    $color = "#cccccc";
    echo "<tr bgcolor=\"" . $color . "\"><td><strong>VIN</strong></td>";
    echo "<td><strong>Year </strong></td>";
    echo "<td><strong>Model </strong></td>";
    echo "<td><strong>Date </strong></td><td></td></tr>";
    echo "<br>";
    
    if(mysql_num_rows($result)){
    while($row = mysql_fetch_assoc($result)){
            
            if ($color == "#cccccc") {
                $color = "#ffffff";
            } else {
                $color = "#cccccc";
            }
            
            echo "<tr bgcolor=\"" . $color . "\"><td>".$row['vin_full']. "</td>
          <td>" . $row['vin_model'] . "</a></td>
              <td>" . $row['vin_year'] . "</a></td>
                  <td>". $row['vreport_timestamp'] . "</a></td>
                      <td><input type=\"hidden\" name=\"u\" value=\"".$username."\"/>
                          <input type=\"hidden\" name=\"rid\" value=\"".$row['report_id']."\"/>
                          </><input type=\"submit\" value=\"View Report\" /></td></tr>\n";
            


        }
    } else {
        echo "<tr><td>No reports on record</td></tr>";
    }
?>
        </table>
    </form>

Thanks!

Dani AI

Generated

Short diagnosis and recap: the form in the original post wraps every row, so multiple controls share the same names and the POST data clashes — PHP will end up with a single value for each non-array name (which is why the last row appeared). and pointed to separate forms or distinct names, and confirmed turning each row into its own form fixed the immediate issue.

A tidy alternative that keeps one form and avoids duplicate field names is to give each row its own submit button whose name/value carry the report id. Only the clicked submit button’s name/value is sent, so the server can detect which report to show:

<!-- per table row -->
<td>
  <button type="submit" name="view_report" value="<?php echo (int)$row['report_id']; ?>">View</button>
</td>

<!-- in reportPDF.php -->
if (isset($_POST['view_report'])) {
    $rid = (int) $_POST['view_report'];
    // validate ownership, then generate/stream the PDF
}

Other workable approaches: keep a single submit and use JS to set a hidden rid before submitting, or keep one small <form> inside the table cell for each row (the approach that solved this thread). Note on markup: wrapping <tr> with <form> can be invalid under some doctypes — place the form inside a <td> or use HTML5’s form="id" attribute on buttons for valid markup.

Security and maintainability reminders: move away from deprecated mysql_* calls to PDO or mysqli with prepared statements; always validate that the logged-in user owns the requested report id before generating the PDF; cast inputs to integers and escape any echoed DB content with htmlspecialchars; protect sensitive actions with CSRF checks.

Recommended Answers

All 3 Replies

Either use new form for each row, or change the field names for each row.

Your form has form elements with the same name in each row so $_POST array contains only the last set of values I guess. Try to enclose form elements that belong to one row in <form></form> tags (so each form has only one submit button).

Great tips! I went and made each row into a Form and that worked perfectly. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.