How To: Insert Data into MySQL Database Using Drop Down Box in PHP
Using PHP and Mysql, we can store data obtained from drop down box into mysql table. The biggest reason for using drop down box is it allows user to only one option only i.e., limited options. A question may arise radio button also gives this functionality of selecting of only one option, but what is special in drop down box? Reason is more options can be given with low area (space in webpage) and can easily fetch by typing first letter of the option.
Read also: How To: Insert Multiple Values Using Dropdown List Box in PHP
Create database ”data” and table “tb”.
1 2 3 4 5 6 7 |
CREATE TABLE `data`.`tb` (`name` VARCHAR( 25 ) NOT NULL , `color` VARCHAR( 5 ) NOT NULL) ENGINE = INNODB; |
Using Global variable $_POST, we have to send data from HTML form into php script.
Here the PHP and HTML coding is to insert the data obtained from drop down box button into MySQL database using PHP script.
index.php
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']))
{
$name=$_POST['name'];
$color=$_POST['color'];
if($name!="" && $color!="")
{
$query=mysql_query("insert into tb values('$name','$color')");
if($query)
{
echo "Updated successfully!";
}
else
{
echo "there is a problem in Databse";
}
}
else
{
echo "Please fill all details";
}
}
else
{
?>
Which Color your like most?
<br />
<form method="post" action="index.php">
Name:<input type="text" name="name" /><br />
<select name="color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Black">Black</option>
<option value="White">White</option>
</select>
<input type="submit" name="submit" />
</form>
<?php
}
?>
</body>
</html>
|
User have to enter their name and their liked color. Both of this fields will be saved in database.





A big thanx for u…
this help me to do my work
thanks sir could u plz send php updation to this mail