gwh Posted June 25, 2007 Share Posted June 25, 2007 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 allows you to choose a garment category associated with the particular garment record. The code below 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: // 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 choose a colour and a 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. Quote Link to comment Share on other sites More sharing options...
gwh Posted June 25, 2007 Author Share Posted June 25, 2007 Just thought I'd give this a try myself and cam eup with the following code: // build array of garment_id/category_id pairs, garment_id/colour_id pairs, and garment_id/size_id pairs one for each category, colour and size $values = array(); foreach ($_POST['category'] as $category_id) && ($_POST['colour'] as $colour_id) && ($_POST['size'] as $size_id){ $values[] = "($garment_id, $category_id)" && "($garment_id, $colour_id)" && "($garment_id, $size_id)"; } // convert array to comma delimited string $values = implode(',',$values); // insert garment_id/category_id, garment_id/colour_id, and garment_id/size_id pairs into garment to category, garment to colour, and garment to size lookup tables $createLookup = 'INSERT INTO garment_to_category (garment_id, category_id) AND INSERT INTO garment_to_colour (garment_id, colour_id) AND INSERT INTO garment_to_size (garment_id, size_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']); } Unfortunately, I got the following error: Parse error: syntax error, unexpected T_BOOLEAN_AND in /Applications/MAMP/htdocs/newsite/admin/insertNewGarment.php on line 104 Line 104 is this line: foreach ($_POST['category'] as $category_id) && ($_POST['colour'] as $colour_id) && ($_POST['size'] as $size_id){ I'm sure the rest of the code block has syntax errors also - can someone help me correct these? Quote Link to comment Share on other sites More sharing options...
fert Posted June 25, 2007 Share Posted June 25, 2007 foreach ($_POST['category'] as $category_id) && ($_POST['colour'] as $colour_id) && ($_POST['size'] as $size_id){ you can't iterate over multiple arrays with foreach. Quote Link to comment Share on other sites More sharing options...
gwh Posted June 25, 2007 Author Share Posted June 25, 2007 Thanks for the info. Any chance of telling me what the corrected code would be? I'm really just a php beginner and am learning as I go. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.