How to Insert Data into MySQL Database Using Radio Button in PHP

By | September 27, 2012

Using PHP and Mysql, we can store data obtained from radio button into mysql table. The biggest reason for using Radio button is radio button allows user to select one option only i.e., limited options.





Read also:  How To: Submit Form When Radio Button is Clicked [Without Submit button]

Create database  ”data” and table “tb”.

1
2
3
4
5
6
7
8
9
10
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 radio button into MySQL database using PHP script.

we have to give all input radio button as color (i.e., all radio buttons should have same radio button )

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<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 />
<input type="radio" name="color" value="Red" />Red
<input type="radio" name="color" value="Green" />Green
<input type="radio" name="color" value="Black" />Black
<input type="radio" name="color" value="White" />White
<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.

8 thoughts on “How to Insert Data into MySQL Database Using Radio Button in PHP

  1. Celine

    Hi i need help to put a radio button (true -false quiz) to my sql database table , can you show me the direction please ?i need help for my assignment , Thanks

    Reply

Leave a Reply

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