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.
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.
Comments