-2

category tbl

cat_id   name    type 
1        cloths   boy
2        cloth    girs
3        bags      boy
4        bags      girls

sub cat. tbl

subcat_id   cat_id    subcat_name
1            (?)        jeans

Now in subcategory table I want to get cat_id.

In subcategory form user can select category->cloth and cat_type->boys... Then I want to add that category id in subcategory table.

Shadow
  • 33,525
  • 10
  • 51
  • 64
Akshay
  • 42
  • 6
  • I don't know on which interface your appliation is running, i assume it's a webapplication? Perhaps you can make a hidden field where you place the category id. When submitting the form read the hidden field so you can insert it in the subcat table. – Kevin Mar 23 '17 at 10:23
  • yes it is web application. But now I'm learner. Please explain me how can I do that.. – Akshay Mar 23 '17 at 10:28
  • Look into [joins](https://dev.mysql.com/doc/refman/5.7/en/join.html). – Nelewout Mar 23 '17 at 12:14
  • Possible duplicate of [How can an SQL query return data from multiple tables](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) – Nelewout Mar 23 '17 at 12:14

1 Answers1

0

Option 1:

On your form add a hidden field to keep the categoryid

<form name="myform" action="<youraction>" method="POST">
 <input type="text" size="25" name="CatName" value="Cloths">
 <input type="text" size="25" name="CatType" value="Boy">
 <input type="hidden" name="CatId" value="1">
</form>

On posting your form, read the value from the hidden field with the name CatId and insert it in the table sub cat.

Option 2:

Before inserting into subcat table, make a query that get's the cat id.

SELECT Cat_Id FROM Cat_tbl WHERE name = 'cloths' AND type = 'boy'

Hope it's a bit clear to you.

Kevin
  • 751
  • 6
  • 12