Jump to content


Photo

php mysql-connect 2 tables

php mysql

  • Please log in to reply
1 reply to this topic

#1 PHP_CHILD

PHP_CHILD

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 02 February 2013 - 10:40 AM

i have 2 tables, items and users.
item has => item_id(unique_1), item_describe,item_cost,item_unit_price
user => user_name, psws, last_name

so whenever user logins inside and adds a item, i want to connect that adding of items with the user.

may b looks simple, but i want to do it without adding any extra field in tables.. is that possible.. Or else if i make item_id as primary, then how should i proceed?


And if item_id is not unique, if 2 users have same items addition with same id,cost_price, how could that be achieved?? thnks in advance...

#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,377 posts
  • LocationCheshire, UK

Posted 02 February 2013 - 11:49 AM

CREATE TABLE useritem (
user_id INT,
item_id INT,
PRIMARY KEY(user_id, item_id)
)

if an item can be owned by one or more users and users can have one or more items then you use a link table to reconcile the many to many relationship

Edited by Barand, 02 February 2013 - 11:51 AM.

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com