0

I've got some code working finally that puts the checked items from two different lists, into their respective tables - all within one form. Everything works fine except...

On the second set of checkboxes, it's putting the final checked item from the array into the database twice. some kinda loop problem? I'm quite new at php, so any help is appreciated. I flipped the foreach statements as a test, and it's still duplicating the very last item. I do an echo statement for checking, and it only shows what I clicked, so I'm not sure where this is happening.

Here's the full code I'm working with:

<?php
if (isset($_POST['submitted'])) {
   $errors = array();
   $item_id = ($_POST['item_id']); 
   $column2 = ($_POST['column2_id']);
   $edible2 = ($_POST['edible2_id']);
   
   if (empty($errors)) {
      require ('databaseconnect.php');
      foreach ($column2 as $val ) {

         $query = "INSERT INTO tablename(plant_id, column2) VALUES ('$item_id', '$val')";
         mysql_query($query) or die( mysql_error() );
       echo $query . '<br>';


      }
    
     foreach ($edible2 as $val ) {
         $query = "INSERT INTO tablename2(plant_id, column2b) VALUES ('$item_id', '$val')";
         mysql_query($query) or die( mysql_error() );
         echo $query . '<br>';


      }


//added this
      $result = mysql_query ($query);



      if ($result) {
         echo 'items have been added';
      }
   } else {
      echo 'system error. Not added';
      echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';
   }
   mysql_close();
}
?>

<body>


<FORM style="border: 1px dotted red; padding: 2px" action="" method="post">

item id field:<br>
only enter numbers here<br>
//this will be a hidden passed variable in finished form

<input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" />
<br><br>


characteristics:<br>
<input type="checkbox" name="column2_id[]" value="1" > one <br>
<input type="checkbox" name="column2_id[]" value="2" > two <br>
<input type="checkbox" name="column2_id[]" value="3" > three <br>
<input type="checkbox" name="column2_id[]" value="4" > four <br>
<input type="checkbox" name="column2_id[]" value="5" > five <br>
<input type="checkbox" name="column2_id[]" value="6" > six <br>
<input type="checkbox" name="column2_id[]" value="7" > Seven <br>


<hr />

characteristics:<br>
<input type="checkbox" name="edible2_id[]" value="1" > one <br>
<input type="checkbox" name="edible2_id[]" value="2" > two <br>
<input type="checkbox" name="edible2_id[]" value="3" > three <br>
<input type="checkbox" name="edible2_id[]" value="4" > four <br>
<input type="checkbox" name="edible2_id[]" value="5" > five <br>
<input type="checkbox" name="edible2_id[]" value="6" > six <br>
<input type="checkbox" name="edible2_id[]" value="7" > Seven <br>

</fieldset><br><br>
<input type="hidden" name="submitted" value="TRUE">
<input type="submit" />

</form>

</body>

</html>
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by turpentyne
0

Neeeeever mind... figured it out. This was duplicating the last entry:

$result = mysql_query ($query);

works now, with this taken out.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.