0

This is my first post here, so go easy on me!

I'm developing a portion of a website that stores problems that arise during business operations, basically a part of CRM. I have everything set where the user can update queries and search for specific ID numbers. The problem I'm having is when I search the DB for ID numbers with a certain account number. I can get a list of them all in table form, but what I want is for the user to be able to click on the ID number and then update it on the update page. What is happening is that only the last row queried is being sent to the update page no matter which ID number is clicked.

Here is my code on the Find page that displays the table:

$querystr = $wpdb->prepare( "SELECT * FROM wp_prs WHERE prsAcct = %s ORDER BY prsID", $account );
$query_results = $wpdb->get_results( $querystr, ARRAY_N );

if ( !isset ($_POST['submit-prs'])) {

// I've left out all css and html that is here

<form action="update.php" method="post">
<?php
$arraySize = count($query_results);
for ( $i = 0; $i < $arraySize; $i++ ) {
    for ( $j = 0; $j < 10; $j++ ) {
        if ( $j == 0 ) {
            $prs_number = $query_results[$i][0];
?>
            <tr>
            <input type="hidden" name="prs-number" value="<?php esc_html_e($prs_number); ?>" />
            <td><input type="submit" name="submit-prs-find" value="<?php esc_html_e($prs_number); ?>" /></td>

<?php
        } else if ( $j == 2 ) {
            $new_date = newDate($query_results[$i][$j]);
            echo '<td>' . $new_date . '</td>';
        } else if ( $j == 5 ) {
            echo '<td id="subject-font">' . $query_results[$i][$j] . '</td>';
        } else if ( ($j >= 6) && ($j <= 9) ) {
            echo '<td>' . substr( $query_results[$i][$j], 0, 20 ) . '...' . '</td>';

        } else {
            echo '<td>' . $query_results[$i][$j] . '</td>';
        }
    }
    echo '</tr>';
}
?>
</form>

I've tried using $_SESSION but still to no avail. Every time I only post the last row ID. Any help would be awesome, I've been stuck on this for a while now and I've exhausted all google searches I can think of.

Thanks in advance!

Tim
  • 3
  • 3
  • 1
    Welcome! You question is quite detailed, which is good. What have you tried with $_SESSION? One option is to pass all of the inputs (but not the query, big security flaw), including the user's selection using a session to the inputs page. This is likely a lot of work on your part, and may introduce new errors, but it could be a backup if you don't have any luck here. – kirkpatt Apr 08 '16 at 22:17
  • you are using one `form` with multiple `submit` and hidden input with the same name. The input with the same name gets overwritten with will make the last one stick. You could move the `
    ` tags around the `input` fields. This will turn every row in a form and send the clicked information.
    – RST Apr 08 '16 at 22:24
  • RST, thanks that worked. I had thought I tried that, but obviously I didn't, I had tried putting the first form tag right before the input, but I was closing in the outer loop instead of the nested one. Really appreciate your response, Thanks again! – Tim Apr 11 '16 at 19:13
  • kirkpatt, I followed one of the examples from [here](http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page), I used the response from Frank. It did work, but because of the form tags not being around only the input tags, I was still getting the last row of the query. Thanks for you help! – Tim Apr 11 '16 at 19:14

1 Answers1

0

You have a Submit for each record, but the <form> is not closed for each one, but surrounding them all.

Try including the <form> </form> within the loop.

You will get loads of submit buttons but maybe that is what you want.

RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • Thanks for the response. I think you were saying the same thing as RST, and it worked perfectly. A lot of submit buttons is what I wanted as the output is a table of ID numbers and snippets of the fields. This way the user can click on the button and open the entire record. Thanks again! – Tim Apr 11 '16 at 19:17
  • Just realised that the
    was not showing so answer didn't make much sense (edited back in). One issue is tha you might want to update a number of records with the one submit, other than having to update one at a time. This is easily done if you want.
    – RGriffiths Apr 12 '16 at 10:38