Jump to content

Select options value


yami

Recommended Posts

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
}
?>

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.