I have two table in my database first is shop_details
and second one is shopOwner_login
in shop_details
table there is column named
ShopOwner_email
Shop_Pass
and same in shopOwner_login
table there is
ShopOwner_Id,
ShopOwner_email,
Shop_Pass
Now i want to make a form in which if user enter data in shop details table like shopowner Email and its password it will store on those two table automatically goes
Can i do this if yes please help me for it i also tried foreign key i am so confused with foreign key because i am new to php and MySql please help me.
2
Answers
Each of your table should have Primary key. Primary Key is the unique id (could be number or string or even uuid. Ex: ShopOwner_Id) which could be used to fetch any row in the table. This primary key is used to establish relationships between tables.
Also for relationships there could be 3 types of major relationships between two tables.
For example,
lets say you have two tables UserLogin and UserProfile. UserLogin contains -> id, email, password, and verified. Whereas UserProfile contains -> id name, address, mobile, dob, etc. Here Each UserLogin will have single UserProfile whereas each UserProfile will have only one UserLogin. So they have One to One relationship. In this case, You add the foreign key to the both tables. You will add profile_id as foreign key in UserLogin whereas login_id in UserProfile.
Lets say, you have two tables
Shop
andUser
. Where eachShop
will belong to a singleUser
(in your case shopowner) But aUser
can have multipleShops
. In this case,Shop
andUser
have Many to One relationship (orUser
andShop
have One to Many relationship). In this case we add a foreign key of user_id (which is primary key ofUser
table) toShop
table.In your case I will suggest to only keep email and password in
shopOwner_login
table and add its foreign key toshop_details
table. This way your data will be normalize and you will not have to make sure to maintain same data in multiple tables.So,d query need to fetch the data looks like –
SELECT a.ShopOwner_email Shop_Pass as EmailId,a.Shop_Pass as Password FROM shop_details as a left join shopOwner_login as b on b.ShopOwner_Id = a.ShopOwner_Id