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.
Here's the code I'm working with:
<?php
if (isset($_POST['submitted'])) {
$errors = array();
$item_id = ($_POST['item_id']); // This was $_POST['plant_id'], which didn't exist in the form.
$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 . '
';
}
foreach ($edible2 as $val2 ) {
$query = "INSERT INTO tablename2(plant_id, column2b) VALUES ('$item_id', '$val2')";
mysql_query($query) or die( mysql_error() );
// echo $query . '
';
}
//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>