gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:18:34 25 4
gpt4 key购买 nike

我正在构建一个带有轻型后台订单管理系统的网站。由于我仍在 php 学习过程中,因此遇到了技术难题。我有一个启动不同脚本的 php 菜单,这个菜单 (waiting_orders.php) 在 html 表中显示等待订单。通过单击按钮中存储的订单主键,用户应该能够使用 order_detail.php 浏览订单详细信息(加入客户表等)。这两个脚本执行正常,但 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'"
);

关于php/pdo/mysql 从 select * in html 表中访问选定的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com