Hi everyone,
I'm trying to build a form which allows for certain information to be entered into a 'garment' table within my database.
I'm trying to create the php code so that it will take care of all the different primary key and foreign key connections. One of the menus in my form allow you to input a garment category associated with the particular garment record. This would insert the category into the 'categories' table within my database, but it also establishes the relationship between the category_id (primary key in category table) and the garment_id (the primary key in the garments table). It does this so that these values can then be inserted in a 'garment_to_category' lookup table. The code for this scenario is as follows:
The problem is that the form also allows you to insert a colour and size. There is a colours table and a sizes table within my database also. There is also garment_to_colour and a garment_to_size lookup tables. Therefore I'd need to revise the above code to establish the relationships as it does for categories and also have the code insert the values into the size and colours lookup tables.
Can someone tell me how I'd alter the code to include these size and colour tables?
Really appreciate any help offered.
I'm trying to build a form which allows for certain information to be entered into a 'garment' table within my database.
I'm trying to create the php code so that it will take care of all the different primary key and foreign key connections. One of the menus in my form allow you to input a garment category associated with the particular garment record. This would insert the category into the 'categories' table within my database, but it also establishes the relationship between the category_id (primary key in category table) and the garment_id (the primary key in the garments table). It does this so that these values can then be inserted in a 'garment_to_category' lookup table. The code for this scenario is as follows:
Code:
// build array of garment_id and category_id pairs, one for each category
$values = array();
foreach ($_POST['category'] as $category_id) {
$values[] = "($garment_id, $category_id)";
}
// convert array to comma delimited string
$values = implode(',',$values);
// insert garment_id/category_id pairs into garment to category lookup table
$createLookup = 'INSERT INTO garment_to_category (garment_id, category_id)
VALUES '.$values;
$result = $db->query($createLookup);
// if successful, redirect to confirmation page
if ($result) {
$db->close();
header('Location: listGarments.php?action=inserted&title='.$_POST['title']);
}
The problem is that the form also allows you to insert a colour and size. There is a colours table and a sizes table within my database also. There is also garment_to_colour and a garment_to_size lookup tables. Therefore I'd need to revise the above code to establish the relationships as it does for categories and also have the code insert the values into the size and colours lookup tables.
Can someone tell me how I'd alter the code to include these size and colour tables?
Really appreciate any help offered.