I've created an orders page for logged in users to see their orders in a table. My 'SELECT' statement was displaying duplicate results at first, but after trying to fix it, it is now showing only one order per user.
I have two tables for the orders orders and ordersdetail.
How do I display all of the users orders instead of just one order or duplicates?
<?php //CUSTOMER $user = $_SESSION['email']; $get_c = "SELECT * FROM customer WHERE email='$user'"; $run_c = $db->query($get_c); $row_c = $run_c->fetch(PDO::FETCH_BOTH); $c_id = $row_c['id']; //ORDERS $get_o = "SELECT * FROM orders WHERE user_id='$c_id'"; $run_o = $db->query($get_o); $row_o = $run_o->fetch(PDO::FETCH_BOTH); $o_id = $row_o['id'];/***/ $o_name = $row_o['name']; $o_status = $row_o['status']; $o_userid = $row_o['user_id']; $o_date = $row_o['date']; //ORDER DETEAILS $get_order = "SELECT * FROM ordersdetail WHERE ordersid='$o_id' "; $run_order = $db->query($get_order); while($row_o = $run_order->fetch(PDO::FETCH_BOTH)){ $order_id = $row_o['ordersid']; $o_price = $row_o['price']; $pro_id = $row_o['productid']; $o_qty = $row_o['quantity']; //PRODUCT DETAILS $get_pro = "SELECT * FROM items WHERE id='$pro_id'"; $run_pro = $db->query($get_pro); $row_pro = $run_pro->fetch(PDO::FETCH_BOTH); $pro_image = $row_pro['photo']; $pro_title = $row_pro['title']; ?> <tr align="center"> <td><?php echo $o_id;?></td> <td> <?php echo $pro_title;?> <b> <img src="images/<?php echo $pro_image;?>" width="50" height="50" /> </td> <td><?php echo $o_price;?></td> <td><?php echo $o_qty;?></td> </tr> <?php } ?> </table> ORDERSDETAIL TABLE
`ordersdetail` ( `id` int(10) NOT NULL, `productid` int(11) NOT NULL, `ordersid` int(11) NOT NULL, `price` float NOT NULL, `quantity` int(11) NOT NULL ) ENGINE=InnoDB ORDERS TABLE
`orders` ( `id` int(10) unsigned NOT NULL, `name` varchar(100) NOT NULL, `status` tinyint(1) NOT NULL, `user_id` int(11) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB
mysql -u YOUR_USERNAME -pand then typeuse YOUR_DB_NAMEand then typeshow tablesto view the tables. This way you can copy the structure in text and simply paste it in your question. If you want to display the structure of a table simply rundesc YOUR_TABLE_NAME.