diasansley Posted February 3, 2011 Share Posted February 3, 2011 hi how do i get the id value from the dropdown selection from the database Here is my code. i need the id to store it another table to refer it in my application. its like a categories and subcategories table. <html> <form id="name" action="<?php $_POST['SERVER_SELF'] ?>" method="POST" > <input type="text" name="name1"> <br> <select name="select"> <?php mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error()); // error message mysql_select_db("dbtest") or die(mysql_error()); $query = "select name,id from main"; $result = mysql_query($query); if ($query) { while ($row = mysql_fetch_array($result)) { $strB=$row['name']; ?> <option value="<?php $stra ?>"><?php echo $strB ?></option> <br> <?php } } ?> </select> <input type="text" name="name2"> <br> <input type="submit" name="submit1"> </form> <?php if(isset($_POST['submit'])) { //$name=$_POST['val']; $strB=$stra; mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error()); // error message mysql_select_db("dbtest") or die(mysql_error()); $query = "insert into sub (sub) values('$strB')"; $result = mysql_query($query); if ($query) { echo "query executed"; } } ?> </html> thanks Quote Link to comment Share on other sites More sharing options...
marcelobm Posted February 3, 2011 Share Posted February 3, 2011 OK, If I understand your question the first thing you need to do is replace this while ($row = mysql_fetch_array($result)) { $strB=$row['name']; ?> <option value="<?php $stra ?>"><?php echo $strB ?></option> <br> <?php } with this while ($row = mysql_fetch_assoc($result)) { $stra = $row['id']; $strB=$row['name']; ?> <option value="<?php echo $stra; ?>"><?php echo $strB; ?></option> <?php } the mysql_fetch_assoc returns an associative array with the column names so you can reference them $row['id'] and $row['name'] then in the value attribute you need to echo the id, and after each <option> tag you don't need to add a <br> tag. as for the processing section you need to retrieve the id trough the $_POST['select'] to get the value selected instead of what you are using now $strB=$stra. And also I would move the processing section to the top of the page. Hope this Helps. Quote Link to comment Share on other sites More sharing options...
diasansley Posted February 3, 2011 Author Share Posted February 3, 2011 wil the code u gave me fetch the id values at run time. depending on what i select in the drop down. Incase i have 10 drop down options will i get the corresponding id when i select it. Thanks Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 Could you explain in more detail what it is you are looking to do for me please? Also is there any errors? What's going wrong? James. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 Also $strB = $stra; $stra does not exist? James. Quote Link to comment Share on other sites More sharing options...
marcelobm Posted February 3, 2011 Share Posted February 3, 2011 Yes, you will get the corresponding ID , Name for each item in the while loop. Quote Link to comment Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 Is this similar to what you are trying to do? <html> <?php mysql_connect('localhost','root','') or die('Could not connect to mysql ' . mysql_error()); mysql_select_db("dbtest") or die(mysql_error()); if(isset($_POST['submit'])) { //$name=$_POST['val']; $id = $_POST['select']; $query = "INSERT INTO `sub` (`sub`) values('" . $id . "')"; $result = mysql_query($query); if($query) { echo "query executed"; } } else { ?> <form id="name" action="<?php $_POST['SERVER_SELF'] ?>" method="POST" > <input type="text" name="name1"><br /> <select name="select" id="select"> <?php $query = "SELECT `name`, `id` FROM `main`"; $result = mysql_query($query); if($query) { while($row = mysql_fetch_array($result)) { $options .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>'; } } echo $options; ?> </select> <input type="text" name="name2"><br /> <input type="submit" name="submit1"> </form> <?php } ?> </html> James. Quote Link to comment Share on other sites More sharing options...
diasansley Posted February 3, 2011 Author Share Posted February 3, 2011 what i am tryin to do is i have two tables one with categories and one with subcategories. i am giving the users a drop down from the categories table before they insert into the subcategories table. Now incase i want to search sub-categories based on categories i need the ID values from the category table to be in the subcategory. The above method was one of the way!! any other more efficient then even better Quote Link to comment Share on other sites More sharing options...
diasansley Posted February 4, 2011 Author Share Posted February 4, 2011 hi james i tried ur code but it dosent seem to work. can u now help me to get the selected name of the checkbox and value. Thanks Quote Link to comment 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.