[英]php/pdo/mysql accessing selected row from a select * in html table
我正在建立一个带有轻型后台订单管理系统的网站。 我遇到了技术难题,因为我还在学习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'"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.