cdoggg94 Posted February 26, 2013 Share Posted February 26, 2013 I have a form that looks like this: <form name="form" action="multiCheck2.php" method="POST" enctype="multipart/form-data"> <table width="381" border="0"> <tr> <td colspan="2">Dates Name: <input name="name" type="text" size="39" /></td> </tr> <tr> <td>Date: <input name="date" type="text" /></td> <td> Time: <input name="time" type="text" /></td> </tr> <tr> <td colspan="2">Additional Information:<br /> <textarea name="comments" cols="45" rows="7"></textarea></td> </tr> <tr> <td> Whos is this for ? <br /> <p> <label> <input type="checkbox" name="who[]" value="Coaches" /> Coaches</label> <br /> <label> <input type="checkbox" name="who[]" value="Conveners" /> Conveners</label> <br /> <label> <input type="checkbox" name="who[]" value="Officials" /> Officials (Both)</label> <br /> <label> <input type="checkbox" name="who[]" value="Ref" /> Ref</label> <br /> <label> <input type="checkbox" name="who[]" value="Linesmen" /> Linesmen</label> <br /> <label> <input type="checkbox" name="who[]" value="Admin" /> Admin</label> <br /> <label> <input type="checkbox" name="who[]" value="All" /> All</label> <br /> </p> </td> <td valign="bottom" align="right"> <br /><br /> <input name="submit" type="submit" value="ADD UPDATE" /></td> </tr> </table> <input type="hidden" name="MM_insert" value="form" /> </form> I have 2 tables I want to interact with. One is called "soccer_dates" and the other is called "soccer_dates_who". I want soccer_dates to take the values (without the date_who) and insert that information. I have this working fine... In the soccer_dates_who, I want to take the new ID from the newly created soccer_dates entry and insert it, along with the the value from the checked boxes. Also, if there is more then one box checked, i want to make it so there is a new entry in soccer_dates_who for each checked box..I have been working on this for a few days and but biggest issue is getting the new ID from soccer_dates and putting it into the new table all in one go. I can do it in 2 different pages, but it adds an extra step.Does anyone have any advice ? Im not really even sure what to look up for examples.. Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/ Share on other sites More sharing options...
cdoggg94 Posted February 26, 2013 Author Share Posted February 26, 2013 I took the enctype="multipart/form-data" out...not sure why it was there.. Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/#findComment-1415108 Share on other sites More sharing options...
Barand Posted February 26, 2013 Share Posted February 26, 2013 Insert into soccer_dates. Call mysqli_insert_id use the returned id when you insert into the who table Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/#findComment-1415121 Share on other sites More sharing options...
cdoggg94 Posted February 26, 2013 Author Share Posted February 26, 2013 ok thanks I am going to look that up and try a few things Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/#findComment-1415125 Share on other sites More sharing options...
cdoggg94 Posted February 26, 2013 Author Share Posted February 26, 2013 I have it working like this: <?php mysql_query("INSERT INTO soccer_dates (date_id, date_name, date_date, date_time, date_content, date_email) VALUES ('', '".$_POST['name']."', '".$_POST['date']."','".$_POST['time']."','".$_POST['comments']."','')"); $parent = mysql_insert_id(); //$whoCat = print_r($_POST['who']); //$whoCat = substr(implode(‘, ‘, $_POST['tags']), 0); foreach($_POST['who'] as $option_key => $option_name) { mysql_query("INSERT INTO soccer_dates_who (who_id, who_parent, who_who) VALUES ('', '".$parent."', '".$option_name."')"); } echo "<br /><br />Thank you for uploading<br /><br />"; ?> Everything seems to be inserting correctly in both tables. Is this a good way of doing it ? or is there an obvious flaw ? Im just happy right now that it's working... Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/#findComment-1415138 Share on other sites More sharing options...
Barand Posted February 26, 2013 Share Posted February 26, 2013 For better performance, avoid queries inside loop. Here's an example of a better method http://forums.phpfreaks.com/topic/274852-need-to-store-multiple-array-from-form-to-mysql/?do=findComment&comment=1414823 Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/#findComment-1415156 Share on other sites More sharing options...
cdoggg94 Posted February 26, 2013 Author Share Posted February 26, 2013 Thanks you much for all the help ! Link to comment https://forums.phpfreaks.com/topic/274972-inserting-into-multiple-tables-and-multiple-entries/#findComment-1415159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.