Simple PHP + MySQL form with radio button and dropdown list

First, create a MySQL database with the following table

CREATE TABLE IF NOT EXISTS 'data' (
  'id' int(10) unsigned NOT NULL,
  'english_name' varchar(50) collate utf8_unicode_ci NOT NULL,
  'tel_number' varchar(8) collate utf8_unicode_ci NOT NULL,
  'email_address' varchar(50) collate utf8_unicode_ci NOT NULL,
  'gender' varchar(1) collate utf8_unicode_ci NOT NULL,
  'age' varchar(10) collate utf8_unicode_ci NOT NULL,
  'region' varchar(10) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 

Prepare a update.php for the MySQL connection.

<?php
  $english_name = $_POST['EnglishName'];
  $tel_number = $_POST['TelNumber'];
  $email_address = $_POST['EmailAddress'];
  $gender = $_POST['Gender'];
  $age = $_POST['Age'];
  $region = $_POST['Region'];

  mysql_connect("[mysql_server]", "[username]", "[password]") or die ('Error: ' . mysql_error());
  mysql_select_db("[database_name]");
	
  $query="INSERT INTO data (english_name, tel_number, email_address, gender, age, region) VALUES (";
  $query.="'".$english_name."', ";
  $query.="'".$tel_number."', ";
  $query.="'".$email_address."', ";
  $query.="'".$gender."', ";
  $query.="'".$age."', ";
  $query.="'".$region."')";

  mysql_query($query) or die ('Error updating database');

  echo "Record is inserted."; 
?>

 

Prepare the form.php

<html>
<head>
  <title>Simple PHP Form</title>
</head>

<body>
  <form method="post" action="update.php">
    <table>
      <tr>
        <td>English Name:</td>
        <td><input type="text" name="EnglishName" size="50" /></td>
        <td>Tel Number:</td>
        <td><input type="text" name="TelNumber" size="8" /></td>
      </tr>
      <tr>
        <td>Email Address:</td>
        <td><input type="text" name="EmailAddress" size="50" /></td>
        <td>Gender:</td>
        <td><input type="radio" name="Gender" value="M" checked="checked" />M<input type="radio" name="Gender" value="F" />F</td>
      </tr>
      <tr>
        <td>Age:</td>
        <td colspan="3">
          <input type="radio" name="Age" value="Under 18" checked="checked" />Under 18
          <input type="radio" name="Age" value="18-23" />18-23
          <input type="radio" name="Age" value="24-30" />24-30
          <input type="radio" name="Age" value="31-40" />31-40
          <input type="radio" name="Age" value="Over 40" />Over 40
        </td>
      </tr>
      <tr>
        <td>Region:</td>
        <td>
          <select name="Region">
            <option value="N/A">N/A</option>
            <option value="HK">Hong Kong</option>
            <option value="KLN">Kowloon</option>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="4"><input type="Submit" value="Submit" /></td>
      </tr>
    </table>
  </form>
</body>
</html>

 

Done =)

16 thoughts on “Simple PHP + MySQL form with radio button and dropdown list”

  1. A very neat and comprehensive example.

    I noticed you have a few Android blogs.

    Any chance you could provide some code to show how it is possible to update or add a record to a MySQL table.

    Would it be possible to write a generalised one so that one could create a form on the android, which could be reference any table you have.

    Thanks

    Richard

    Like

  2. if your having issues with the mysql code this also works
    just replace “MYDB with the database that your putting the table in

    CREATE TABLE  `MYDB`.`data` (
    `id` INT( 10 ) NOT NULL ,
    `english_name` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
    `tel_number` VARCHAR( 8 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
    `email_address` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
    `gender` VARCHAR( 1 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
    `age` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
    `region` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
    PRIMARY KEY (  `id` )
    ) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci; 
    

    Liked by 1 person

  3. My radio group looks like this:

    Are you part of the corporate/group challenge?
    Register below:

    Bank of America
    Rancho Simi Recreation and Park District
    Simi Valley Hospital
    City of Simi Valley
    Simi Valley Unified School District

    Bank of America

    Rancho Simi Recreation and Park District

    Simi Valley Hospital

    City of Simi Valley

    Simi Valley Unified School District

    What is wrong with this code? What do I need to do to get it into the database?

    Linda

    Like

      1. One more help plz:
        1.I need to be able to login into mysql database using one time login password, let say i generate a password which can be valid only for two hours, then after that it expires, until when i use another one.

        Like

      2. Hi ykyuen, here is the elaboration: I need to set a login password in mysql table, which will expire after a certain time, let say 2 hours only. Is there any software or script which ca be able to generate such type of passwords? I am developing online quiz so i need users to use passwords only within a limited time of session, there after expires and request another pasword. I hope now it is clear.

        Like

    1. You can add a created at column in the table you store the login name and password so that you can store the created timestamp when the user requests the password.

      When the user attempts to login, you could compared the current timestampe and the stored created at timestamp.

      If the difference is bigger than 2 hours, Reject his login request.

      Does it sound feasible?

      Like

  4. how can i create database for checkbox input,when form is submitted and then inserted to database, my code is below..plz guide me here

    body {
        background-image:url("img.jpg");
    }
    
    form data
    
    
    
    
    <!-- create Table-->
    	
    	    <!-- start first row-->
    		
    			<b>Personal information</b>
    		<!-- end first row-->
    
    		
    <!-- start second row-->		
    
    	<b>Name</b>
    		
    			
    				
    				
    			
    			<!-- end second row-->
    			
    			
    
    	<b>Address</b>
    		
    			
    				
    				
    				
    			
    
    	
    	
    
    
    
    
    	<b>Phone no.</b>
    		
    
    	
    		
    
    	<b>Employment Type</b>
    
    
    
    	Full time
    	part time
    	Contract
    	Remote
    	
    
    
    	corp to corp
    	corp to Hire
    	w2
    
    
      
    	  <b>
    	   Experience Details
    	  
        
    Total number of experience
        
    		
    			
    				select any
    				fresher
    				internship
    				2 yrs
    				5 yrs
    				10 yrs
    			
    		
    	
    	
    
    	
    		<b>
    			skills Details
    		</b>
    	
    
    
    skillyears
    
    
    	
    	
    
    
    
    	last used
    		
    	
    			
    	<!-- start Education Details row-->
    <b>Education Details</b>
    DegreeInstitution
    
    
    				select any
    				10+2
    				Bachelor
    				Master
    				P.H.D
    			
    
    
    	
    	Locationyear of Graduation
    		
    			
    		<!-- End Education Details row-->
    
    <b>Employment details</b>
    	companypositionlocation
    	
    	
    	
    	start DateEnd Date
    	
    	
    
    <!-- End of Table-->
    

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.