d3xt3r
d3xt3r

Reputation: 130

How can i retrieve the ordered products by user, and display them? using SESSION PHP

How can i retrieve the ordered products by user, and display them?

I have 2 Tables: Orders_good & Users, if someone register all wil go into users, and if someone order a product everything will go in orders_good.

Everything is working fine, but except 1 thing.

After the user will submit a order, i want that they will see their ordered products on their page.

So let say i want to do this : If the user is logged in, then search the name in the Table: ('orders_good").. And if find that name in that table then search if he has some products orders, if yes: then show the orders, row by row, if no then show: No orders Yet .....

The table: orders_good, haves 2 columns: user_name & product_order

I am tyring with this, the User_Name session is working.

But how can i show the users their ordered products row by row???

$sql = "SELECT * FROM orders";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}


session_id($id);
session_start();
echo $_SESSION['user_name'];
echo $row["product_order"];

This is the DB structure:

enter image description here

Upvotes: 1

Views: 86

Answers (1)

Frank B
Frank B

Reputation: 3697

Okay,

First a general warning. Mysql functions are deprecated. Use Mysqli or PDO.

Then.. You should not link tables with usernames. What will happen if you have two users with te same name? things would get mixed through each other.

So the first thing to do is to give your user table and your order table a primary key with autoincrement. And call this column user_id and order_id.

So this would be your user table:

  • user_id (primary key / autoincrement / integer)
  • name (varchar)
  • email (varchar)
  • ...

For your orders table you need a "foreign key" to your users table.

this foreign key stores the user_id .

So this would be your order table:

  • order_id (primary key / autoincrement / integer)
  • user_id (integer)
  • order_date (datetime)
  • ...

You will also need a orderrule table which stores one article that belongs to your order

  • orderrule_id (primary key / autoincrement / integer)
  • order_id (integer)
  • quantity (integer)
  • description (varchar)
  • amount (decimal 10,2)

schematic:

user 1: --> order 1: --> orderrule 1
                     --> orderrule 2
                     --> orderrule 3

        --> order 2: --> orderrule 4
                     --> orderrule 5

user 2: --> order 3: --> orderrule 6
                     --> orderrule 7
                     --> orderrule 8
                     --> orderrule 9

        --> order 4: --> orderrule 10

Upvotes: 3

Related Questions