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 =)

About these ads

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

  1. Richard

    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

    Reply
  2. Blake

    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; 
    
    Reply
  3. Linda Martins Mann

    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

    Reply
      1. Bonny

        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.

      2. Mrbonnytz

        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.

    1. ykyuen Post author

      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?

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s