How To: Insert Multiple Values Using Dropdown List Box in PHP

By | August 11, 2012

In normal dropdown, user can select only one option in the drop down menu. Both Check box and multiple drop down box works same as each other. The reason for using multiple drop down box, instead of  using check box is check box will take more space for options whereas dropdown box takes less space for any number of options.

Create database table with two fields (id and name).

[code type=sql]

CREATE TABLE `freeze`.`tb` (
`id` INT( 3 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )

) ENGINE = INNODB;

[/code]

Here is the php code to insert more than one values into mysql table.

Give <select> html attribute  name in array, that array value is imploded (i.e seperated) with (,) and imploded value is stored in $choice1

[code type=php]

<html>
<body>
<?php
if(isset($_POST[‘submit’]))
{
$query=mysql_connect(‘localhost’,’root’,”);
mysql_select_db(“freeze”,$query);
$choice=mysql_real_escape_string($_POST[‘game’]);
$choice1=implode(‘,’,$choice);
mysql_query(“insert into tb values(”,’$choice1′)”);
}
?>
<form method=”post” action=”index.php”>
Select your favourite game:<br/>
<select name=”game[]” multiple=”multiple”>
<option>Football</option>
<option>Volleyball</option>
<option>Badminton</option>
<option>Cricket</option>
</select>
<input type=”submit” name=”submit”>
</form>
</body>
</html>

[/code]

6 thoughts on “How To: Insert Multiple Values Using Dropdown List Box in PHP

  1. neha

    Could you help me for the following
    How To Insert Multiple Values Using Check Box in PHP. The data which is checked should properly store in mysql db
    Thanks in advance

    Reply
  2. suresh

    Could you help me for the following
    How To Insert Multiple Values Using Single Text Box in PHP then move to multiple row data in MYSQL
    Thanks in advance

    Reply

Leave a Reply to jaysingh Cancel reply

Your email address will not be published. Required fields are marked *