Logo

32.4K views Asked 29 Feb 2020 in PHP
4

Hello Friends 

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

If I check the male radio button then the male value should be inserted into the MySQL database and if I check the female radio button then the female value should be inserted into the database. 

My Mysql database structure. 

 

I am attaching a screenshot of the PHP code. 

This operation is related to inserting data using the HTML radio button. 

User osama (90 Points)

2 Answers
2
shakti (9.4K Points)

answered 29 Feb 2020 (edited)

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

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 the POST method 

$gender=$_POST['ab'];

Insert this $gender variable value into the database. 

Note -

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

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


Give me more reference.  –  osama 29 Feb 2020
No need to attach JAVA screenshot if you are working on PHP. Create a simple HTML form with two radio buttons. Provide the same name for both radio buttons. like ab , ab or gen gen . Like - ab name for first radio button ab name for the second radio button The value should be different Provide value for male value="male" Provide value for female radio button value="female" Use the post method to handle the form value and insert it into the database using the insert query. Follow the simple process. Reference https://technosmarter.com/php/php-sign-up-form.html  –  shakti 29 Feb 2020
Add comment

2
vishalrana1 (7.4K Points)

answered 29 Feb 2020

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 the 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 it in the comment. 

 


It works. I want to say that - Thanks for it.  –  osama 01 Mar 2020
Add comment

Your Answer
×
Login Required

You must login to continue.

Login Google Login with Google
Register