futrose Posted July 27, 2011 Share Posted July 27, 2011 Here is the set up. I have 3 tables elders with id/elder fields (and other fields but not important for question), eldercat with id/category fields, and eldercategory with elderid/eldercatid fields. The id's from the elders and eldercat tables are joined/associated in the eldercategory table. Ok, so this is what I am trying to do. I have a drop down list populated from my eldercat table at the top of my form showing available elder categories. The form is used to add new staff/elders members to the churches database. I am able to add an elder to the database and have them associated with 1 category. I'm not sure how to set it up so that if multiple categories are selected it will add the staff member id to the eldercategory table and associate it with both staff categories selected. Here is the code snippets. index.php include '../../includes/db.inc.php'; $uploadname = mysqli_real_escape_string($link, $_FILES["file"]["name"]); $LastName = mysqli_real_escape_string($link, $_POST['LastName']); $HFirst = mysqli_real_escape_string($link, $_POST['HFirst']); $WFirst = mysqli_real_escape_string($link, $_POST['WFirst']); $position = mysqli_real_escape_string($link, $_POST['position']); $email = mysqli_real_escape_string($link, $_POST['email']); $email2 = mysqli_real_escape_string($link, $_POST['email2']); $bio = mysqli_real_escape_string($link, $_POST['bio']); $avail = mysqli_real_escape_string($link, $_POST['available']); $orderby = mysqli_real_escape_string($link, $_POST['orderby']); $eldercatid = mysqli_real_escape_string($link, $_POST['eldercatid']); $sql = "Insert into elders set image = '$uploadname', LastName = '$LastName', HFirst = '$HFirst', WFirst = '$WFirst', position= '$position', email = '$email', email2 = '$email2', bio = '$bio', avail = '$avail', orderby = '$orderby'"; if (!mysqli_query($link, $sql)) { $error = 'Database error storing elder download!'. mysqli_error($link); include '../../includes/error.php'; exit(); } $result = mysqli_query($link, 'Select id from elders order by id DESC LIMIT 1'); if (!$result) { $error = 'Error fetching elder id: ' . mysqli_error($link); include '../../includes/error.php'; exit(); } while($row = mysqli_fetch_array($result)) { $id = ($row['id']); } $sql = "Insert into eldercategory set eldercatid = '$eldercatid', elderid = '$id'"; if (!mysqli_query($link, $sql)) { $error = 'Database error storing elder and category id!'. mysqli_error($link); include '../../includes/error.php'; exit(); } header('Location: .'); exit(); } elders_modify.php <form action="?<?php htmlout($action); ?>" method="post" enctype="multipart/form-data"> <table cellspacing="5" cellpadding="1" border="0"> <tr> <td>Staff Category:</td><td><select name="eldercatid" multiple> <?php foreach ($eldercat as $ec): ?> <option value="<?php echo $ec['id']; ?>"><?php echo $ec['category']; ?></option> <?php endforeach; ?> </select></td> </tr> <tr> <td>Husband Name:</td><td> <input type="text" name="HFirst" id="HFirst" value="<?php htmlout($HFirst); ?>" size="30" /></td> </tr> <tr> <td>Wife Name:</td><td> <input type="text" name="WFirst" id="WFirst" value="<?php htmlout($WFirst); ?>" size="30" /></td> </tr> <tr> <td>Last Name:</td><td> <input type="text" name="LastName" id="LastName" value="<?php htmlout($LastName); ?>" size="30" /></td> </tr> <tr> <td>Active:</td><td> <select name ="available"> <option value="Y">Yes</option> <option value="N">No</option> </select> </td> </tr> <tr> <td>Order By:</td><td> <input type="text" name="orderby" id="orderby" value="<?php htmlout($orderby); ?>" size="2" /></td> </tr> <tr> <td>Position: </td><td><input type="text" name="position" id="position" value="<?php htmlout($position); ?>" size="30" /> <br /> </td> </tr> <tr> <td>Email: </td><td><input type="text" name="email" id="email" value="<?php htmlout($email); ?>" size="20" /> <br /> </td> </tr> <tr> <td>Email2: </td><td><input type="text" name="email2" id="email2" value="<?php htmlout($email2); ?>" size="20" /> <br /> </td> </tr> <tr> <td>Bio: </td><td><textarea name="bio" id="bio" rows="13" cols="70"><?php htmlout($bio); ?></textarea></td> </tr> <tr> <td>Select image to upload: </td> <td> <input type="file" id="file" name="file" value="<?php htmlout($image); ?>"/> </td> </tr> <tr> </td> <td> <input type="hidden" name="id" value="<?php htmlout($id); ?>" /> <input type="submit" name="<?php htmlout($button); ?>" value="<?php htmlout($button); ?>" /></td> </tr> </table> </form> any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/242903-put-id-from-multiple-selections-from-a-drop-down-list-in-db/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.