Multiple Selection in to a MySQL table
Drop down select only one option and no any issues with sending single value into a MySQL table.But look at bellow multiple select box.
We could select multiple item listed here by using shift key.with simple scripting you could also sent the selected values in to a MySQL table.I have created table called multiple in my_database as follows
CREATE TABLE IF NOT EXISTS `multiple` (
`id` int(30) NOT NULL AUTO_INCREMENT,
`multy_name` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Then created above html file.
<form action="send.php" method="post"> <select name="test[]" multiple="multiple"> <option value="one">one</option> <option value="two">two</option> <option value="three">three</option> <option value="four">four</option> <option value="five">five</option> </select> <input type="submit" value="Send" /> </form> |
send.php Which will be sending data into MySQL table is as bellow
<?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database", $con); $test=$_POST['test']; if ($test){ foreach ($test as $t){ mysql_query("INSERT INTO multiple VALUES ('', '$t')"); } } echo "<font color='red'>successfull!</font>"; mysql_close($con); ?> |
Stored data looks like bellow in the table.
No comments:
Post a Comment