Problems with edit order page

I have encountered a situation when the code is correct, but there are problems showing edit_order.php which is in the admin panel.

I also tried using $order instead of $r but it didn’t change anything

The following message appears
Notice: Undefined variable: order in /opt/lampp/htdocs/e-shop/admin/edit_order.php on line 54

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/e-shop/admin/edit_order.php on line 54

include 'header.php';
include '../server/connection.php';

if (isset($_GET['order_id'])) {
	$order_id = $_GET['order_id'];
	$stmt = $conn->prepare("SELECT * FROM orders WHERE order_id=?");
	$stmt->bind_param('i', $order_id);

	$order_result = $stmt->get_result();

} else if (isset($_POST['edit_order'])) {

	$order_status = $_POST['order_status'];
	$order_id = $_POST['order_id'];

	$stmt = $conn->prepare("UPDATE orders SET order_status=? WHERE order_id=?");
	$stmt->bind_param('si', $order_status, $order_id);

	if ($stmt->execute()) {
		header('Location: index.php?order_updated=Order has been updated successfully');
	} else {
		header('Location: products.php?order_failure=Error occurred, please try again');
} else {
	header('Location: index.php');

<div class="container-fluid">
    <div class="row" style="min-height: 200px">

        <?php include 'sidemenu.php';?>

        <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                <h1 class="h2">Dashboard</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <div class="btn-group me-2"></div>

            <h2>Edit Order</h2>
            <div class="table-responsive">

                <div class="mx-auto container">
                    <form id="edit-order-form" method="POST" action="edit_order.php">

                        <?php foreach ($order as $orders) {

                        <p style="color: red;"><?php if (isset($_GET['error'])) {echo $_GET['error'];}?></p>

                        <div class="form-group mt-2">
                            <label>Order Id</label>
                            <p class="my-4"><?php echo $r['order_id']; ?></p>

                        <div class="form-group my-3">
                            <label>Order Price</label>
                            <p class="my-4"><?php echo $r['order_cost']; ?></p>

                        <input type="hidden" name="order_id" value=""/>

                        <div class="form-group my-3">
                            <label>Order Status</label>
                            <select class="form-select" required name="order_status">
                                <option value="not paid"><?php if ($r['order_status'] == 'not paid') {echo "selected";}?>Not paid</option>
                                <option value="paid"><?php if ($r['order_status'] == 'paid') {echo "selected";}?>Paid</option>
                                <option value="shipped"><?php if ($r['order_status'] == 'shipped') {echo "selected";}?>Shipped</option>
                                <option value="delivered"><?php if ($r['order_status'] == 'deliverd') {;}?>Delivered</option>

                        <div class="form-group my-3">
                            <label>Order Date</label>
                            <p class="my-4"><?php echo $r['order_date']; ?></p>

                        <div class="form-group mt-3">
                            <input type="submit" class="btn btn-primary" name="edit_order" value="Update"/>
                    <?php }?>

<script src="" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="" integrity="sha384-fbbOQedDUMZZ5KreZpsbe1LCZPVmfTnH7ois6mU1QK+m14rQ1l2bGBq41eYeM/fS" crossorigin="anonymous"></script>
<script src="" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>

The variable you are trying to loop over isn’t even the same name you are using for the query result.

Also, this code has the same problem as in your previous thread. You have conditional logic that controls when code runs to query the database to get data, but you are blindly attempting to loop over the result from that query. I recommend that you reread the replies you got in that thread.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.