yami Posted April 2, 2022 Share Posted April 2, 2022 I am trying to put a column from a database table into a select input .How can I catch the value that I chose using php? Here is my code.Thanks in advance <?php $stmt = $con->query("SELECT size FROM size INNER JOIN inventory ON size.idSize = inventory.idsize WHERE id_item =$idd"); while($row = $stmt->fetch_assoc()) { ?> <form action='' method="post"> <select name='size' id="size" class='size'> <option value="<?php $row ?>"><?php echo $row['size'] ?></option> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/ Share on other sites More sharing options...
Barand Posted April 2, 2022 Share Posted April 2, 2022 Use prepared statements instead of putting user-provided variables directly in to the query <?php $stmt = $con->prepare("SELECT DISTINCT size FROM size INNER JOIN inventory ON size.idSize = inventory.idsize WHERE id_item = ? ORDER BY size "); $stmt->bind_param('i', $idd); $stmt->execute(); $stmt->bind_result($size); $opts = ""; while($row = $stmt->fetch() { $opts .= "<option value='$size'>$size</option>\n"; } ?> <form action='' method="post"> <select name='size' id="size" class='size'> <?=$opts?> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/#findComment-1594833 Share on other sites More sharing options...
yami Posted April 2, 2022 Author Share Posted April 2, 2022 okay but how can I catch each chosen value since all values are in one variable?sorry I am still a beginner Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/#findComment-1594836 Share on other sites More sharing options...
ginerjm Posted April 2, 2022 Share Posted April 2, 2022 You are trying to create a drop-down list of the items found with your query, correct? Do you want the user to be able to pick just one item or several? If you want to allow multiple picks at once you need to add that option to your select tag and then use an array name for the name attribute of the tag. Then your php can process that array instead of just one item. Look up the tag in an html reference to learn about the multiple select. Then change the name on your select tag to 'size[]' to make it an array. Then in the code that will handle the POST input you will use a loop on the 'size' array to collect all of the items picked. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/#findComment-1594837 Share on other sites More sharing options...
Barand Posted April 2, 2022 Share Posted April 2, 2022 When your form is submitted (you need a Submit button) the chosen option will be in $_POST['size'] 1 Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/#findComment-1594838 Share on other sites More sharing options...
yami Posted April 2, 2022 Author Share Posted April 2, 2022 Ah okay thanks! Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/#findComment-1594839 Share on other sites More sharing options...
ginerjm Posted April 2, 2022 Share Posted April 2, 2022 HTH! Good luck. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314658-select-options-value/#findComment-1594840 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.