How to Insert a Registration Form Into MySQL

To capture and store information from a registration form on a web page in a MySQL database, you need to know the MySQL database name, username and password to manipulate the database. Using a text editor, you need to edit the web page, create the registration form in HTML and create a PHP script that extracts the form data and inserts it into the MySQL database when the user clicks to submit the HTML form.


  1. Create Registration Form

    • 1

      Launch a text editor and create a new file. Type in the HTML code to display the registration form. For example, type:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

      <html xmlns="">


      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>My Web Page</title>



      <strong>Registration Form</strong>

      <form id="registration" name="registration" method="post" action=""><input name="submit" type="hidden" value="1" />


      &lt;label>Name: &lt;/label>

      &lt;input name="name" type="text" size="40" />


      <p><label>Email: </label><input name="email" type="text" size="60" /></p>


      &lt;input type="submit" name="submit" id="submit" value="Submit" />





    • 2

      Save the file as an "html" file, for example, "registration.html." Create a new file in the text editor for the PHP script that will process the form information.

    • 3

      Enter the PHP code to capture the field information and insert it into the MySQL database. For example, type:



      Script to process form data and insert it into a MySQL database,

      All input strings must be escaped with mysql_real_escape_string to prevent

      MySQL Injection Attacks


      if ($_POST["submit"] != 1) return; // Check to see that form was submitted

      // Get name and email data from form

      $name = $_POST["name"];

      $email = $_POST["email"];

      // Open MySQL database and select the registration table

      $dbc = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die(mysql_error());

      $db = mysql_select_db("registration", $dbc);

      // Search the registration table to see if the email already exists in the database

      $query = 'SELECT * FROM registration WHERE email="' . mysql_real_escape_string($email) . '"';

      $result = mysql_query($query);

      if ($result) {

      echo "Email already exists in database!";



      // Insert the name and email into the database

      $query = 'INSERT INTO registration (id, name, email) VALUES (NULL, "' . mysql_real_escape_string($name) . '", "' . mysql_real_escape_string($email) . '")';

      $result = mysql_query($query);

      if (!$result)

      echo "&lt;p>Error inserting into database " . mysql_error() . "&lt;/p>";


      echo "&lt;p>Registration data successfully stored in database!&lt;/p>";


    • 4

      Save the file as "registration.php." Open the "registration.html" file, insert "registration.php" into the "action" value and save the file. For example:

      <form id="registration" name="registration" method="post" action="registration.php">

    • 5

      Click "Start -- Internet Explorer." Enter the URL of the website into the address bar of the browser, followed by "/registration.html." Test the registration form by entering information and verifying through the messages on the screen that the data is being added to the MySQL database.

Related Searches


You May Also Like

Related Ads