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).

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `freeze`.`tb` (
`id` INT( 3 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` )

) ENGINE = INNODB;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<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>

5 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

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

aspersive-expediency