Inserting radio button value to mysql database through php script

In the below example shows that  “Inserting radio button value to mysql database through php script”
Before we begin let us understand brief about radio buttons, radio buttons are used to select single value from a group or it is for single selection or true / false type of values.

Step 1 : Create table as following screen shot in MySql database with all fields set to varchar type.

Step 2 : Create a configuration file for connection purpose as following and name it config2.php (as per my example)

<?php
      $host="localhost";
      $username="root";
      $password="";
      $dbname="demo";
      $con=mysql_connect("$host","$username","$password");
      mysql_select_db("$dbname",$con);
?>

Step 3:  Create main html/php page to display radio buttons and name it “radio2mysql.php” and follow the below code.

<html>
<head> 
</head> 
<body>
<center>
<table style="border: 1px solid yellowgreen;">
 <h2>Inserting Radio button value to MySQL database in 
php</h2>
 <form method="post" action="">
<tr> 
<td> Register No: </td> <td> <input type=text 
name=t1> </td> </tr>
<tr><td>  <input type="radio" name="status" 
value="Booked">Booked 
 <input type="radio" name="status" 
value="Pending">Pending </td> </tr>
<tr> <td> 
<input type="submit" name="submit" Value="submit"> 
</td> </tr> </table>
 </form>
 </body>
</html>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
include 'config2.php';
$t1=$_POST['t1'];
$st=$_POST['status'];
$query=mysql_query("select * from booked_status where 
regno='".$t1."' ") or die(mysql_error());
$duplicate=mysql_num_rows($query);
if (isset($_POST['submit']))
{
if($duplicate==0)
   {
$query = "INSERT INTO booked_status (regno, status) 
VALUES ('$t1','$st')";
mysql_query($query) or die(mysql_error());
echo "<center><h2 style='background-color:green; 
color:white; display:inline'><span>Successfully 
updated.</span>";
 }
 else
    {
      echo "<center><h2 style='background-color:red; 
color:white; display:inline'><span>The Register No. '$t1' is 
already present in the booking table";
    }
}
?>

Step 4: Output of the above program is…

Step 5: After successful submit with register number and radio button value following output can be generated.

Step 6 : After unsuccessful submit with register number and radio button value following output can be generated(duplicate values).
Thank you for referring above example!
Share
Share
Scroll to Top