How to Insert Radio button value into database in PHP MySQL

By Super Admin | May 15, 2021 | PHP
Share :

https://www.fundaofwebit.com/post/how-to-insert-radio-button-value-into-database-in-php-mysql

Insert Radio button value into database in PHP MySQL


In this tutorial, you will be learning how to insert radio button values into database using php mysql, which the data will be passed from the form to mysql query using POST method in php to insert data from radio button values in php mysql.

We will be using Bootstrap v5 for designing the user inteface for radio button value insert data in php.

Lets get started to insert radio button value in mysql table using php:

Note: For better and detailed understanding. please watch out the Video above.

Step 1: Create a table named demo into your Database (MySQL) as follows:

CREATE TABLE `demo` (
    `id` int(11NOT NULL,
    `name` varchar(191NOT NULL,
    `gender` varchar(20NOT NULL
);

Step 2: Create a index.php file and paste the below html form design code:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Funda of Web IT</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">

                <?php 
                    if(isset($_SESSION['status']))
                    {
                        ?>
                            <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <strong>Hey!</strong> <?php echo $_SESSION['status']; ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            </div>
                        <?php
                         unset($_SESSION['status']);
                    }
                ?>

                <div class="card mt-5">
                    <div class="card-header">
                        <h4>How to Insert RADIO Button value into Database in php</h4>
                    </div>
                    <div class="card-body">

                        <form action="code.php" method="POST">
                            <div class="form-group mb-3">
                                <label for="">Name</label>
                                <input type="text" name="name" class="form-control" />
                            </div>
                            <div class="form-group mb-3">
                                <label for="">Gender</label> <br>
                                <input type="radio" name="gender" value="Male" /> Male
                                <input type="radio" name="gender" value="Female" /> Female
                            </div>
                            <div class="form-group mb-3">
                                <button type="submit" name="save_radio" class="btn btn-primary">Save Radio Data</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Now, when you click on the Submit button, the <form> contains action="code.php" so, lets write the code:

Step 3: Create a code.php file and paste the below code to insert radio value in MySQL Database in php.

<?php
session_start();
$con = mysqli_connect("localhost","root","","phptutorials");

if(isset($_POST['save_radio']))
{
    $name  = $_POST['name'];
    $gender  = $_POST['gender'];

    $query = "INSERT INTO demo (name,gender) VALUES ('$name','$gender')";
    $query_run = mysqli_query($con$query);

    if($query_run)
    {
        $_SESSION['status'] = "Inserted Successfully";
        header("Location: index.php");
    }
    else{
        $_SESSION['status'] = "Inserted Successfully";
        header("Location: index.php");
    }
}
?>



Thanks for reading...

https://www.fundaofwebit.com/post/how-to-insert-radio-button-value-into-database-in-php-mysql

Share this blog on social platforms