简体   繁体   English

php / pdo / mysql从html表中的select *访问所选行

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

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. 我遇到了技术难题,因为我还在学习PHP的过程中。 I have a php menu which launches different scripts and this one (waiting_orders.php) displays waiting orders in a html table. 我有一个启动不同脚本的php菜单,这个(waiting_orders.php)在html表中显示等待命令。 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). 通过单击存储在按钮中的订单主键,用户应该能够使用order_detail.php(加入客户端表等)浏览订单详细信息。 The two scripts execute fine but order_detail.php doesn't show expected result. 这两个脚本执行正常,但order_detail.php没有显示预期的结果。

waiting_orders.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 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. 您可能猜到,order_detail.php始终显示最后一行,而不是用户选择的当前orderID。 I tried using an array but without success as I don't really see how to handle it. 我尝试使用数组但没有成功,因为我没有真正看到如何处理它。 Thanks. 谢谢。

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. 在您的代码中,您将orderID存储到$_SESSION['order_key'] ,然后使用$_SESSION变量在表单中输出orderID。

Try this. 尝试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM