Page 1 of 1

Stumped by form submit of multiple entries to two tables

Posted: Wed Jul 21, 2010 6:56 pm
by turpentyne
I'm new to php and sql, so bear with me.

I'm trying to submit an item's number and it's various characteristics into one many-to-many link table, then to submit the same item number and it's uses into another link table.

I'm stumped right now. It seems to be working, entering the information into both tables, including the multiple entries from the checkboxes into the uses table. but i get an error stating

Duplicate entry '22-1' for key 1
query:INSERT INTO item_uses_link(item_id, use_id) VALUES ('22', '1'), ('22', '2'), ...

Here is my code:

<?php

if (isset($_POST['submitted'])) {
$errors = array();

$descriptor1 = trim($_POST['item_id']);
$descriptor3 = trim($_POST['characteristics_id']);

$descriptor4 = trim($_POST['1']);
$descriptor5 = trim($_POST['2']);
$descriptor6 = trim($_POST['3']);
$descriptor7 = trim($_POST['4']);
$descriptor8 = trim($_POST['5']);
$descriptor9 = trim($_POST['6']);
$descriptor10 = trim($_POST['7']);
$descriptor11 = trim($_POST['8']);
$descriptor12 = trim($_POST['9']);
$descriptor13 = trim($_POST['10']);
$descriptor14 = trim($_POST['11']);
$descriptor15 = trim($_POST['12']);
$descriptor16 = trim($_POST['13']);

if (empty($errors)) {

require ('3_z_mysq1_c0nn3ct.php');

$query = "INSERT INTO plant_characteristics_link(item_id, characteristic_id)
VALUES ('$descriptor1', '$descriptor3')";

$result = @mysql_query ($query);


$query = "INSERT INTO item_uses_link(item_id, use_id)
VALUES ('$descriptor1', '$descriptor4')";
VALUES ('$descriptor1', '$descriptor5')";
VALUES ('$descriptor1', '$descriptor6')";
VALUES ('$descriptor1', '$descriptor7')";
VALUES ('$descriptor1', '$descriptor8')";
VALUES ('$descriptor1', '$descriptor9')";
VALUES ('$descriptor1', '$descriptor10')";
VALUES ('$descriptor1', '$descriptor11')";
VALUES ('$descriptor1', '$descriptor12')";
VALUES ('$descriptor1', '$descriptor13')";
VALUES ('$descriptor1', '$descriptor14')";
VALUES ('$descriptor1', '$descriptor15')";
VALUES ('$descriptor1', '$descriptor16')";



$result = @mysql_query ($query);

if ($result) {

echo 'Items have been added';


exit();

} else {
echo 'system error. No item 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 style="border: 1px dotted red; padding: 2px" action="insertMultipletables1e.php" method="post"><fieldset><legend><b>Enter your info here</b></legend>


<br>
<table bgcolor=#FFEC8B width=590>
<tr><td>


plant id field:<br>
only enter numbers here<br>

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


</td>
<td>

// this is the part that doesn't work. I don't get 1 - 13 on 13 rows. Just one entry and a 0 in the column

<input type="checkbox" name="use 1" value="<?php if(isset($_POST['1'])) echo $_POST['1']; ?>" > use 1 <br>
<input type="checkbox" name="use 2" value="<?php if(isset($_POST['2'])) echo $_POST['2']; ?>" > use 2 <br>
<input type="checkbox" name="use 3" value="<?php if(isset($_POST['3'])) echo $_POST['3']; ?>" > use 3 <br>
<input type="checkbox" name="use 4" value="<?php if(isset($_POST['4'])) echo $_POST['4']; ?>" > use 4 <br>
<input type="checkbox" name="use 5" value="<?php if(isset($_POST['5'])) echo $_POST['5']; ?>" > use 5<br>
<input type="checkbox" name="use 6" value="<?php if(isset($_POST['6'])) echo $_POST['6']; ?>" > use 6 <br>
<input type="checkbox" name="use 7" value="<?php if(isset($_POST['7'])) echo $_POST['7']; ?>" > use 7 <br>
<input type="checkbox" name="use 8" value="<?php if(isset($_POST['8'])) echo $_POST['8']; ?>" > use 8 <br>
<input type="checkbox" name="use 9" value="<?php if(isset($_POST['9'])) echo $_POST['9']; ?>" > use 9 <br>
<input type="checkbox" name="use 10" value="<?php if(isset($_POST['10'])) echo $_POST['10']; ?>" > use 10 <br>
<input type="checkbox" name="use 11" value="<?php if(isset($_POST['11'])) echo $_POST['11']; ?>" > use 11 <br>
<input type="checkbox" name="use 12" value="<?php if(isset($_POST['12'])) echo $_POST['12']; ?>" > use 12 <br>
<input type="checkbox" name="use 13" value="<?php if(isset($_POST['13'])) echo $_POST['13']; ?>" > use 13 <br>

<br>
</td>
</tr>
</table>

<br>

<table bgcolor=#FFEC8B width=590>
<tr>
<td>

characteristics field:<br> <SELECT name="characteristics_id" value="<?php if(isset($_POST['characteristics_id'])) echo $_POST['characteristics_id']; ?>">
<OPTION SELECTED VALUE=""> </OPTION>
<OPTION VALUE="1">1
<OPTION VALUE="2">2
<OPTION VALUE="3">3
</select>
<br>
</td>

</tr></table>

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

</form>

Re: Stumped by form submit of multiple entries to two tables

Posted: Wed Jul 21, 2010 8:09 pm
by requinix
"Duplicate entry" means just that: a duplicate entry. For one reason or another that's forbidden in the table.

Not a PHP problem.

If such a constraint shouldn't exist then remove the key.