Jump to content

Recommended Posts

I'm rather new at this, so bear with me if this is obvious and simple. 

 

Now that many-to-many databases have sunk into my brain, I'm trying to build a form that puts multiple entries into two different 'link' tables for one item. One for its physical characteristics and one for its uses.

 

Last night I fairly quickly figured out the multiple table entry, by simply duplicating the Insert command.. Trying the same thing to enter into the same table didn't work. Here's a simple test code I've got so far.. The final will probably be check fields for each item that goes into the link table along with its item id. ( I took the code out I'd written to try and go into the same table.)

 

 

<?php

 

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

$errors = array();

 

//I just add more descriptors here for new characteristics or uses listed in link table

 

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

$descriptor2 = trim($_POST['characteristics_id']);

$descriptor3 = trim($_POST['use_id']);

if (empty($errors)) {

 

//this is my connection to database

require ('3_z_mysq1_c0nn3ct.php');

 

$query = "INSERT INTO item_characteristics_link(item_id, characteristics_id)

VALUES ('$descriptor1', '$descriptor2')";

 

$result = @mysql_query ($query);

 

//here I go into the second table - the next 5 lines are what I tried duplicating - didn't work.

if (isset($_Post['uses_id']))

 

$query = "INSERT INTO item_uses_link(item_id, uses_id)

VALUES ('$descriptor1', '$descriptor3')";

 

if ($result) {

 

echo 'your info has 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="backToThisSameFile.php" method="post"><fieldset><legend><b>Enter your info here</b></legend>

 

 

<table bgcolor=#FFEC8B width=590>

<tr><td>

 

//in the final form, this will not be a dropdown, but a unique number passed from another form.

Item id field:<br><SELECT NAME="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" >

<OPTION SELECTED VALUE=""> </OPTION>

<OPTION VALUE="1">1

<OPTION VALUE="2">2

<OPTION VALUE="3">3

<OPTION VALUE="4">4

<OPTION VALUE="5">5

<OPTION VALUE="6">6

</select><br>

 

 

</td>

<td>

//in the final form, this will not be a dropdown, but a series of individual checkboxes/entries

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

<OPTION VALUE="4">4

<OPTION VALUE="5">5

<OPTION VALUE="6">6

</select>

<br>

another item/entry<br>

another item/entry

</td>

 

</tr></table>

 

<br><br>

 

<table bgcolor=#FFEC8B width=590>

<tr>

<td>

//in the final form, this will not be a dropdown, but a series of individual checkboxes/entries

uses field:<br> <SELECT name="uses_id" value="<?php if(isset($_POST['uses_id'])) echo $_POST['uses_id']; ?>">

<OPTION SELECTED VALUE=""> </OPTION>

<OPTION VALUE="1">1

<OPTION VALUE="2">2

<OPTION VALUE="3">3

<OPTION VALUE="4">4

<OPTION VALUE="5">5

<OPTION VALUE="6">6

</select>

<br>

</td>

 

</tr></table>

 

 

</fieldset><br><br>

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

<input type="submit" />

 

 

</form>

Hmmm... I might've found the code that will help me. I'll have to plug it in this evening, adapt the form and see if it works. Something like this, maybe:

 

INSERT INTO ''item_characteristics_link'' (''item_id'', [''characteristics_id''])

VALUES (''Item 1'', [''blue'']),

(''item 1'', [''circular''])

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.