turpentyne Posted July 21, 2010 Share Posted July 21, 2010 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> Link to comment https://forums.phpfreaks.com/topic/208433-one-form-multiple-entries-on-two-tables-yikes/ Share on other sites More sharing options...
turpentyne Posted July 21, 2010 Author Share Posted July 21, 2010 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'']) Link to comment https://forums.phpfreaks.com/topic/208433-one-form-multiple-entries-on-two-tables-yikes/#findComment-1089210 Share on other sites More sharing options...
turpentyne Posted July 21, 2010 Author Share Posted July 21, 2010 dang! didn't work. I added the code in with checkboxes for one table, and it's not working. The first table updates, but the second one does nothing. Link to comment https://forums.phpfreaks.com/topic/208433-one-form-multiple-entries-on-two-tables-yikes/#findComment-1089251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.