php/pdo/mysql accessing selected row from a select * in html table

devoteur

I'm building a website with a light back-office order management system. I am encountering a technical difficulty as I'm still in the php learning process. I have a php menu which launches different scripts and this one (waiting_orders.php) displays waiting orders in a html table. By clicking on the order primary key stored in a button, the user should be able to browse the order details with order_detail.php (join client table and so on). The two scripts execute fine but order_detail.php doesn't show expected result.

waiting_orders.php

    <?php   
session_start();
if(isset($_POST['detail']))
    { header('Location: order_detail.php'); }
?>
<!DOCTYPE html>
all html stuff
    <?php // all db connexion stuff
        $status = "waiting";
        $select = $connexion -> prepare("SELECT orderID,order_date,order_qty,order_amount
        FROM Orders WHERE status = '$status'"
        );
        $select->execute();
        $result = $select->fetchall();
        echo '<table cellpadding="0" cellspacing="0" class="db-table">';
        echo '<tr><th>Order no</th><th>Date</th><th>Quantity</th><th>Amount</th></tr>';

        foreach($result as $row)
        {
            $_SESSION['order_key'] = $row['orderID']; // orderID is the primary key

            echo '<tr>';
            echo '<form method="post" action=""><td><button class="btn btn-danger bold" type="submit" name="detail" value="'.$_SESSION['order_key'].'">'.$row['cmdID'].'</button></td></form>';
            echo '<td>',$row['order_date'],'</td>';
            echo '<td>',$row['order_qty'],'</td>';
            echo '<td>',$row['order_amount'],'</td>';
            echo '</tr>';
        }                           
        echo '</table>';
?>

order_detail.php

    <?php 
session_start();
$select_key = $_SESSION['order_key'] ;
?>
<!DOCTYPE html>
all html stuff
<?php  // all db connexion stuff
$select = $connexion -> prepare(
"SELECT * FROM Orders WHERE orderID = '$select_key'"  
);

As you may guess, order_detail.php always displays last row and not the current orderID selected by the user. I tried using an array but without success as I don't really see how to handle it. Thanks.

guyver4mk

In your code, you are storing the orderID into $_SESSION['order_key'] and then using the $_SESSION variable to output the orderID in the form.

Try this.

waiting_orders.php

    <?php   
session_start();
?>
<!DOCTYPE html>
all html stuff
    <?php // all db connexion stuff
        $status = "waiting";
        $select = $connexion -> prepare("SELECT orderID,order_date,order_qty,order_amount
        FROM Orders WHERE status = '$status'"
        );
        $select->execute();
        $result = $select->fetchall();
        echo '<table cellpadding="0" cellspacing="0" class="db-table">';
        echo '<tr><th>Order no</th><th>Date</th><th>Quantity</th><th>Amount</th></tr>';

        foreach($result as $row)
        {
            $_SESSION['order_key'] = $row['orderID']; // orderID is the primary key

            echo '<tr>';
            echo '<form method="post" action="order_detail.php'"><td><button class="btn btn-danger bold" type="submit" name="detail" value="'.$row['orderID'].'">'.$row['cmdID'].'</button></td></form>';
            echo '<td>',$row['order_date'],'</td>';
            echo '<td>',$row['order_qty'],'</td>';
            echo '<td>',$row['order_amount'],'</td>';
            echo '</tr>';
        }                           
        echo '</table>';
?>

order_detail.php

    <?php 
session_start();
$select_key = $_POST['detail'];
/*
    You can also set the $_SESSION variable here if it is needed elsewhere
*/
// $_SESSION['order_key'] = $_POST['detail'];
?>
<!DOCTYPE html>
all html stuff
<?php  // all db connexion stuff
$select = $connexion -> prepare(
"SELECT * FROM Orders WHERE orderID = '$select_key'"  
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

php/pdo/mysql accessing selected row from a select * in html table

From Dev

Display table row based on the selected option of Select html tag

From Dev

Select a row from html table and send values onclick of a button

From Dev

Select a row (highlight) from html table and send values onclick of a button

From Dev

How to select a particular value in row from a table in html

From Dev

Select a row (highlight) from html table and send values onclick of a button

From Dev

HTML Table do not add row until all present text box are filled and select is selected

From Dev

highlight selected row and column on mouseover in html table

From Dev

highlight selected row and column on mouseover in html table

From Dev

mysql - how to select skew row from selected

From Dev

Getting the ID of selected table in HTML select

From Dev

Issue on Drawing Highcharts From Selected Row Table

From Dev

Delete selected row from table in mysql

From Dev

iOS : Copy selected row from Table to an Array:

From Dev

Delete selected row from table in mysql

From Dev

How to iterate through selected HTML table rows and retrieve data from each row

From Dev

Remove checkbox selected from non-selected table row

From Dev

Selected value from html select menu

From Dev

PHP HTML select box selected from MYSQL

From Dev

Select row from table from each schema

From Dev

Select row from table from each schema

From Dev

Matlab add selected row data from child table to parent table

From Dev

Select multiple selected values from a column from each row

From Dev

Retriving selected value from cell in HTML table

From Dev

How to delete a selected row in a html table by using jQuery?

From Dev

Highlight selected table row

From Dev

How select row from html table and get the value of the first cell and put it on PHP Session

From Dev

SQl: Update table with last row from a select

From Dev

Select highest revision from table for each row

Related Related

  1. 1

    php/pdo/mysql accessing selected row from a select * in html table

  2. 2

    Display table row based on the selected option of Select html tag

  3. 3

    Select a row from html table and send values onclick of a button

  4. 4

    Select a row (highlight) from html table and send values onclick of a button

  5. 5

    How to select a particular value in row from a table in html

  6. 6

    Select a row (highlight) from html table and send values onclick of a button

  7. 7

    HTML Table do not add row until all present text box are filled and select is selected

  8. 8

    highlight selected row and column on mouseover in html table

  9. 9

    highlight selected row and column on mouseover in html table

  10. 10

    mysql - how to select skew row from selected

  11. 11

    Getting the ID of selected table in HTML select

  12. 12

    Issue on Drawing Highcharts From Selected Row Table

  13. 13

    Delete selected row from table in mysql

  14. 14

    iOS : Copy selected row from Table to an Array:

  15. 15

    Delete selected row from table in mysql

  16. 16

    How to iterate through selected HTML table rows and retrieve data from each row

  17. 17

    Remove checkbox selected from non-selected table row

  18. 18

    Selected value from html select menu

  19. 19

    PHP HTML select box selected from MYSQL

  20. 20

    Select row from table from each schema

  21. 21

    Select row from table from each schema

  22. 22

    Matlab add selected row data from child table to parent table

  23. 23

    Select multiple selected values from a column from each row

  24. 24

    Retriving selected value from cell in HTML table

  25. 25

    How to delete a selected row in a html table by using jQuery?

  26. 26

    Highlight selected table row

  27. 27

    How select row from html table and get the value of the first cell and put it on PHP Session

  28. 28

    SQl: Update table with last row from a select

  29. 29

    Select highest revision from table for each row

HotTag

Archive