php/pdo/mysql 从 select * 在 html table 中访问 selected 行

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

我正在构建一个带有轻型后台订单管理系统的网站。由于我仍在 php 学习过程中,我遇到了技术难题。 我有一个启动不同脚本的 php 菜单,而这个菜单 (waiting_orders.php) 在 html table 中显示等待订单。通过点击存储在一个按钮中的订单主键,用户应该能够浏览带有order_detail.php的订单详情(加入客户端table等等)。这两个脚本执行正常,但 order_detail.php 未显示预期结果。

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'"  
);

您可能猜到了,order_detail.php 始终显示最后一行而不是用户选择的当前 orderID。我尝试使用数组但没有成功,因为我真的不知道如何处理它。谢谢。

在您的代码中,您将 orderID 存储到 $_SESSION['order_key'] 中,然后使用 $_SESSION 变量在表单中输出 orderID。

试试这个。

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'"  
);