TechieClues TechieClues
Updated date Jul 13, 2023
This article provides a detailed explanation of how to insert multiple radio button values into a MySQL database using PHP. It covers the step-by-step process, including creating the necessary database tables, writing the MySQL query, developing a PHP program to handle the form submission, and displaying the output.
  • 3.1k
  • 0
  • 0

Download File(s):

PHP_radiobutton.zip

Introduction:

In web development, it is common to collect user input through forms and store the data in a database. Radio buttons are a popular form element that allows users to select a single option from a set of choices. In this article, we will explore how to insert multiple radio button values into a database using PHP. We will cover the process step-by-step, including creating the necessary database tables, writing the MySQL query, developing a PHP program to handle the form submission, and finally, displaying the output.

Step 1: MySQL Query Creation

Before diving into the PHP implementation, we need to create the necessary database tables to store the radio button values. Let's assume we have a table named "users" with the following structure:

CREATE TABLE users (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  gender VARCHAR(10) NOT NULL
);

In this example, we have two columns: "name" to store the user's name and "gender" to store their selected gender.

Step 2: Create an HTML form

Now, let's move on to the PHP implementation. We will create an HTML form that contains radio buttons for gender selection and a submit button. When the user submits the form, the PHP script will handle the form data and insert it into the database.

Create a new HTML file and name it "index.html" and Copy the provided HTML form code into the "index.html" file.

<!DOCTYPE html>
<html>
<head>
  <title>Radio Button Form</title>
</head>
<body>
  <form method="POST" action="submit.php">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required><br><br>

    <label for="gender">Gender:</label>
    <input type="radio" id="male" name="gender" value="Male" required>
    <label for="male">Male</label>
    <input type="radio" id="female" name="gender" value="Female" required>
    <label for="female">Female</label><br><br>

    <input type="submit" value="Submit">
  </form>
</body>
</html>

Step 3: Create the PHP script (submit.php)

Create a new PHP file and name it "submit.php" and Copy the provided PHP script code into the "submit.php" file.

<?php
// Establish a database connection
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve form data
$name = $_POST['name'];
$gender = $_POST['gender'];

// Prepare and execute the MySQL query
$sql = "INSERT INTO users (name, gender) VALUES ('$name', '$gender')";

if ($conn->query($sql) === TRUE) {
    echo "Data inserted successfully.";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

// Close the database connection
$conn->close();
?>

Replace "your_username", "your_password", and "your_database" with your actual MySQL database credentials in the "submit.php" file. Save both files and place them in your web server's document root directory.

In the PHP script, we start by establishing a connection to the MySQL database using the provided credentials. Then, we retrieve the form data using the $_POST superglobal and store them in variables. Next, we prepare an SQL query to insert the data into the "users" table using the INSERT INTO statement. We execute the query using the $conn->query() method and check if it was successful. Finally, we close the database connection.

Output:

When a user fills out the form and submits it, the PHP script will insert the data into the database and display an appropriate message.

If the insertion is successful, the message "Data inserted successfully" will be shown; otherwise, an error message will be displayed.

To verify the insertion of data, you can open MySQL and execute the SQL query "SELECT * FROM users". This query will retrieve all the records from the "users" table and display the results as shown below:

Conclusion:

In this article, we learned how to insert multiple radio button values into a database using PHP. We covered the necessary steps, including creating the database tables, writing the MySQL query, developing the PHP program, and displaying the output. By following the step-by-step instructions, you can easily incorporate radio button values into your database-driven web applications and enhance the user experience.

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!