Welcome to Techno Smarter QA, Questions, Answers Community
+4 votes
180 views

Hello Friends 

I Want to insert value using HTML radio button in PHP with MYSQL database . For example -

If i check male radio button then male value should be insert into mysql database and if i check female radio button then female value should be insert into the database . 

My Mysql database structure . 

I am attaching screenshot for PHP code . 

This operation is related to inserting data using html radio button . 

in Programming by (90 points) 13 31

2 Answers

+2 votes

If you want to add a radio button then you should create a form with a radio button. 

For example - 

<input type="radio" name="ab" value="Male">Male 

<input type="radio" name="ab'" value="Female"> Female

Use PHP code and POST radio data using POST method 

$gender=$_POST['ab'];

Insert this $gender variable value into database. 

Note -

1. The radio button name should the same for both. 

2. Use the radio button name to insert data by POST Method 

by (8.9k points) 8 22 61
edited by
Give me more reference .
No need to attach JAVA screenshot if you are working on PHP . Create a simple html form with two radio buttons . Provide same name for both radio button . like ab , ab or gen gen .

Like - ab name for first radio button
           ab name for second radio button


 Value should be different

Provide value for male       value="male"
 Provide vale for female radio button  value="female"

Use post method to handle form value and insert into database using insert query .

Follow the simple process ..

Reference

https://technosmarter.com/php/php-sign-up-form.html
+1 vote

Thanks for the question here. I am here to help you. You are not described well with your HTML and PHP code. You should not include a code screenshot. You should include code only with description. I am understanding your concern. You want to insert radio button data like gender male or female in the database using PHP and MYSQL database. It's really simple. 

Let's follow the steps - 

How to insert data using the radio button in PHP? 

Step 1 - First of all create a database using the query 

CREATE TABLE  `data` (

 `id` INT( 50 ) NOT NULL ,

 `name` VARCHAR( 40 ) NOT NULL ,

 `gender` VARCHAR( 30 ) NOT NULL ,

); 

In the query above, I created three-columns and a table name . We use the gender column and will insert male or female values.

Step 2- Now you need to create an HTML form with one input box and two radio buttons. 

Let's have a look - 

<form action="" method="post"

Name:-

<input type="text" name="name"><br><br>

Gender :-

<input type="radio" name="gender" value="Male">Male 

<input type="radio" name="gender" value="Female">Female 

<input type="submit" value="Submit" name="submit">

</form>

Step 3- If you want to insert gender in database, you have to create PHP code with MYSQL query

Let's create a PHP script to insert gender Male or Female in PHP. 

 <?php 

$databaseHost = 'localhost';

$databaseName = 'records'; 

$databaseUsername = 'root'; 

$databasePassword = '';  

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);

if(isset($_POST['submit']))

{

$name = $_POST['name'];

$gender = $_POST['gender'];

$result = mysqli_query($mysqli,"insert into data values('','$name','$gender')");

if($result)

{

echo "Data inserted ";

}

else{

echo "Something wrong";

}

}

?>

Execute the code above. In this way, you can insert gender male or female in the database. 

If you have another issue, you can discuss in the comment. 

by (6.7k points) 7 27 60
It works.
I want to say that - Thanks for it.
...