Home >>MySQL Tutorial >How to edit radio button in PHP MySQL

How to edit radio button in PHP MySQL

How to update a value of radio button in database

In this tutorial you will learn How to update user's profile using PHP MySQL(focused radio button) How to update a value of radio button in database (mysql) using PHP and MySQL Simple Script.

Create database, Create table, insert Values

First create a database name "demo" select database demo. Create a table "update_radio_button_value". Insert some data inside table "update_radio_button_value".
//create a database
CREATE DATABASE `demo`;

//select database
USE demo;
 
//Create table 
CREATE TABLE `demo`.`update_radio_button_value`
 
(

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`name` CHAR( 50 ) NOT NULL ,

`email` VARCHAR( 50 ) NOT NULL ,

`mobile` BIGINT NOT NULL ,

`gender` ENUM( "m", "f" ) NOT NULL

);

//Insert Values
INSERT INTO `demo`.`update_radio_button_value`

(

`id` ,`name` ,`email` ,`mobile` ,`gender`

)

VALUES (NULL , 'Sanjeev', 'Sanjeev@gmail.com', '9015501897', 'm');

PHP Script Fetch and Show Data

First Connect With Database through MySQL I fetch user's data and display the data in HTML form field i.e text,email,number and radio button. all data will display in text filed, email filed and number field using value property except radio button. For radio button first need to check the value from database then "checked" the radio button.
<?php
$connect= new mysqli("localhost","root","","demo") or die("ERROR:could not connect to the database!!!");

//select users data
$query=$connect->query("select * from update_radio_button_value");

//fetch data
$result=$query->fetch_array();

?>

<style>

input{width:250px}

input[type=submit]{width:120px; height:25px}

table{padding:5px}

</style>

<form method="post" action="update.php">

<table style="background:#F8F8F8" border="0" align="center">  
  
  <tr>

    <td height="35">Name</td>

    <td><input type="text" name="n" value="<?php echo $result['name'];?>" /></td>

  </tr>
  
  <tr>

    <td width="58" height="40">Email</td>

    <td width="256"><input readonly="readonly" type="email" name="eid" value="<?php echo $result['email'];?>" /></td>

  </tr>

  <tr>

    <td height="42">Mobile</td>

    <td><input type="number" name="mob" value="<?php echo $result['mobile'];?>" /></td>

  </tr>

  <tr>

    <td height="70">Gender</td>

    <td>

	  Male
	    <input type="radio" name="gen" value="m" <?php if($result['gender']=="m"){ echo "checked";}?>/>

	    <BR/>

	  Female
	    <input type="radio" name="gen" value="f" <?php if($result['gender']=="f"){ echo "checked";}?>/>

      </td>

  </tr>
 
    <tr>

    <td colspan="2" align="center">

		<input type="submit" value="Update My Profile" name="update"/>

	</td>

  </tr>
  
</table>

</form>

PHP Script for update user's data

On this Page retrieve data from previous Page through HTML Form and update the data. After update the user's data redirect the user on same page from where he came and show recently updated data.
<?php

$connect= new mysqli("localhost","root","","demo") or die("ERROR:could not connect to the database!!!");

extract($_POST);

if(isset($update))

{

$query=$connect->query("update update_radio_button_value SET name='$n',mobile='$mob',gender='$gen' where email='$eid'");

	if($query)

	{

	header('location:get_radio_button_checked_value.php');

	}

}

?>

No Sidebar ads