Anna Simona
Anna Simona

Reputation: 77

Show last database entry for user

This is kindda of a hard one. The whole story: I have a page where users register and login. in database they get a member_id value (auto increasing) this database is "users". So when the user wnats to order something the data of the item and his member_id get into another database let's say "orders". In this database is sotred the info about he product and his member_id. So when the user would hit the button check-out he would have to come to a page where his LAST added item would be displayed sorted by his member_id

So something like, show me the last item entry for this user.

I don't know if the current session that i am using $_SESSION['SESS_MEMBER_ID'] must be compaird to the member_id table in the database to identify this users last entry.

To be honest i have no idea how i should do this.

I hope this wasn't to confusing... :)

Edit: Table code:

CREATE TABLE IF NOT EXISTS orders ( 
  order_id int(11) NOT NULL, 
  Items text NOT NULL, 
  member_id text NOT NULL )
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

Upvotes: 0

Views: 105

Answers (5)

user169600
user169600

Reputation:

Your structure for your table orders is "wrong", member_id should ideally have the same settings like in your users table, int(11) I guess. So your table should look like this:

CREATE TABLE IF NOT EXISTS orders ( 
  order_id int(11) NOT NULL, 
  Items text NOT NULL, 
  member_id int(11) NOT NULL )
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

As for selecting a users orders latest item, try this SQL:

SELECT * FROM orders WHERE member_id = [XYZ] ORDER BY order_id DESC LIMIT 1; similar to other answers here. Replace [XYZ] with a member_id. If that doesn't work in your code, make sure to try this query in phpMyAdmin (or CLI etc.).

Upvotes: 1

Vivek Singh
Vivek Singh

Reputation: 2447

you can take member id in session (if its not) and than you can just get last entry of this member from order(if member_id is present in order table) table by using the following mysql query

$sql=mysql_query("select max(`id`) from `orders` where member_id='".$_SESSION['member_id']."'");

Upvotes: 0

makjal
makjal

Reputation: 15

You can fetch max id from table where id would be auto increment. then fetch the record of table for that id :

$no="select max(id) from table";
$no1=mysql_fetch_row($no);
$sql="select * from table where id=$no1[0]";
$a=mysql_fetch_row($sql);

Upvotes: 0

I'm Geeker
I'm Geeker

Reputation: 4637

You need to use orderby

select * from orders where  member_id = 'Member ID' order by order_num desc limit 0,1

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

If you want the last entry for the user from orders table you can use the following as

select * from orders
where 
member_id = ?
order by order_num desc limit 1

Upvotes: 0

Related Questions