Jump to content

revising code


gwh

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/57041-revising-code/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/57041-revising-code/#findComment-281854
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.