Jump to content

one form - two tables - multiple checkboxes


turpentyne

Recommended Posts

ok... Can somebody give me a rundown how to build a form that submits multiple rows to two different tables.  I've been trying to write this out and making absolutely no progress. I'm thinking maybe I'll start from scratch if anybody can give a basic rundown of how to do this.

 

Below is essentially what I've got so far.  The medical table drops every single checkbox in, whether I checked it or not, and it only puts zeros in the second column. I only want a row created for each thing checked.

 

The edible table is doing nothing. No errors though. Just not dropping anything in. I've tried a dozen different ways of doing this and I've gone cross-eyed. Maybe a clear mind can tell me how to do this.

 

 

<?php

 

if (isset($_POST['submitted'])) {

$errors = array();

 

$descriptor1 = trim($_POST['plant_id']);

 

$descriptor4 = trim($_POST['alternative']);

$descriptor5 = trim($_POST['aromatherapy']);

$descriptor6 = trim($_POST['bach']);

 

$descriptor17 = trim($_POST['chocolate_substitute']);

$descriptor18 = trim($_POST['coffee_substitute']);

$descriptor19 = trim($_POST['coloring']);

 

 

 

if (empty($errors)) {

 

require ('3_z_mysq1_c0nn3ct.php');

 

 

$query = "INSERT INTO plant_edible_link(plant_id, edible_id)

VALUES ('$descriptor1', '$descriptor17'),

('$descriptor1', '$descriptor18'),

('$descriptor1', '$descriptor19'),

 

 

$result = mysql_query ($query);

 

 

$query = "INSERT INTO plant_medicine_link(plant_id, medicine_id)

VALUES ('$descriptor1', '$descriptor4'),

('$descriptor1', '$descriptor5'),

('$descriptor1', '$descriptor6'),

 

 

$result = @mysql_query ($query);

 

if ($result) {

 

echo 'one plant has been added';

 

 

exit();

 

} else {

echo 'system error. No plant added';

 

echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';

 

exit();

 

}

mysql_close();

 

} else {

 

echo 'error. the following error occured <br>';

foreach ($errors as $msg) {

 

echo " - $msg<br>\n";

 

}

 

} // end of if

 

} // end of main submit conditional

 

?>

 

 

<FORM  action="insertMultipletables1f.php" method="post">

<br>

Plant relationships, part 2<br>

 

<table bgcolor=#FFEC8B width=590>

<tr><td>

 

 

plant id:<br>

only enter numbers here<br>

 

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

<br>

 

 

 

Medical Uses:<br>

<input type="checkbox" name="alternative" value="1" > Alterative <br>

<input type="checkbox" name="aromatherapy" value="2" > Aromatherapy <br>

<input type="checkbox" name="bach" value="3" > Bach <br>

 

 

<br><br>

 

 

 

<table bgcolor=#FFEC8B width=590>

<tr>

<td>

Edible Uses:<br>

 

<input type="checkbox" name="chocolate_uses" value="14" > Chocolate substitute<br>

<input type="checkbox" name="coffee_substitute" value="24" > Coffee substitute<br>

<input type="checkbox" name="coloring" value="34" > Coloring <br>

 

 

</fieldset><br><br>

<input type="hidden" name="submitted" value="TRUE">

<input type="submit" />

 

 

</form>

Link to comment
https://forums.phpfreaks.com/topic/208559-one-form-two-tables-multiple-checkboxes/
Share on other sites

I do not see , there is an issue with form post.  Only when i check the values posted and correct values too. Please let us know where you have faced the error.

 

<?php

 

if (isset($_POST['submitted'])) {

$errors = array();

 

$descriptor1 = trim($_POST['plant_id']);

 

$descriptor4 = trim($_POST['alternative']);

$descriptor5 = trim($_POST['aromatherapy']);

$descriptor6 = trim($_POST['bach']);

 

$descriptor17 = trim($_POST['chocolate_substitute']);

$descriptor18 = trim($_POST['coffee_substitute']);

$descriptor19 = trim($_POST['coloring']);

 

 

 

if (empty($errors)) {

print_r($_POST);

 

//require ('3_z_mysq1_c0nn3ct.php');

 

 

/*$query = "INSERT INTO plant_edible_link(plant_id, edible_id)

VALUES ('$descriptor1', '$descriptor17'),

('$descriptor1', '$descriptor18'),

('$descriptor1', '$descriptor19'),

 

 

$result = mysql_query ($query);

 

 

$query = "INSERT INTO plant_medicine_link(plant_id, medicine_id)

VALUES ('$descriptor1', '$descriptor4'),

('$descriptor1', '$descriptor5'),

('$descriptor1', '$descriptor6'),

 

 

$result = @mysql_query ($query);*/

 

if ($result) {

 

echo 'one plant has been added';

 

 

exit();

 

} else {

echo 'system error. No plant added';

 

echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';

 

exit();

 

}

//mysql_close();

 

} else {

 

echo 'error. the following error occured <br>';

foreach ($errors as $msg) {

 

echo " - $msg<br>\n";

 

}

 

} // end of if

 

} // end of main submit conditional

 

?>

 

 

<FORM  action="form1.php" method="post">

<br>

Plant relationships, part 2<br>

 

<table bgcolor=#FFEC8B width=590>

<tr><td>

 

 

plant id:<br>

only enter numbers here<br>

 

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

<br>

 

 

 

Medical Uses:<br>

<input type="checkbox" name="alternative" value="1" > Alterative <br>

<input type="checkbox" name="aromatherapy" value="2" > Aromatherapy <br>

<input type="checkbox" name="bach" value="3" > Bach <br>

 

 

<br><br>

 

 

 

<table bgcolor=#FFEC8B width=590>

<tr>

<td>

Edible Uses:<br>

 

<input type="checkbox" name="chocolate_uses" value="14" > Chocolate substitute<br>

<input type="checkbox" name="coffee_substitute" value="24" > Coffee substitute<br>

<input type="checkbox" name="coloring" value="34" > Coloring <br>

 

 

</fieldset><br><br>

<input type="hidden" name="submitted" value="TRUE">

<input type="submit" />

 

 

</form>

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.