How to enter form data into database

I got this project dropped in my lap, and I’am not quite sure how to move forward with it.
Even though I work as a computer tech, my field is hardware, malware removal and networking. PHP programming is totally new to me.

The company I work for is a not-for-profit organization, we provide medical services for people who can not afford to go to the regular doctor. We need a way to keep track of our patients and their visits.

I have a form created, with name, address, phone number, zip code, but I do not know how to get that information into the database.

I also need a way to make sure the information is not a duplicate.

Here is an example, that is close to what you need. Assuming we are working with MySQL database. First, create a file named db.php where we will define several constants to store MySQL database information, as well as connect to the database:


// Database settings


// Connecting to MYSQL database

$mysqlink=mysql_connect(HostName,UserName,Password) or die(mysql_error());


Next, let’s create a script that we will use just one time - to create database structure. In this example we will create just one table contacts. We will name this file create.php:


// connecting to MySQL database


// Creating MySQL database

if(!mysql_query("CREATE DATABASE IF NOT EXISTS ".DBName)){ echo mysql_error();}
else { echo ‘Database created.
’; }

// Creating table in MySQL database

mysql_query("CREATE TABLE contacts(

ID                    int(  4) not null auto_increment primary key,

ContactName       varchar(128) default '',
Address           varchar(128) default '',
City              varchar( 64) default '',
State             varchar(  2) default '',
Zip               varchar( 10) default '',

Phone             varchar( 32) default '',
Email             varchar( 32) default ''

){ echo mysql_error();}
else { echo ‘Table “contacts” created.
’; }


Now, all we need to create database and table - run this script. I.e. from your browser open

And finally, we will create a form and a script that will add new record to our contacts table, when the form is submitted.
We will name this file form.php.





// Validate form data

if($_POST["contactname"]=='') $err.='Please enter Contact Name<br>';
if($_POST["email"]=='') $err.='Please enter Email<br>';


  // Check if there are duplicate entries in the 'contacts' table

  $r=mysql_query("SELECT ID FROM contacts WHERE ContactName='".addslashes($_POST["contactname"])."' and Email='".addslashes($_POST["email"])."'");
    $err.='Can not add duplicate entry<br>';

    // adding new record to 'contacts' table

    mysql_query("INSERT INTO contacts (ContactName,Address,City,State,Zip,Phone,Email) 
                 values ('".addslashes($_POST["contactname"])."','".addslashes($_POST["address"])."','".addslashes($_POST["city"])."','".addslashes($_POST["state"])."','".addslashes($_POST["zip"])."','".addslashes($_POST["phone"])."','".addslashes($_POST["email"])."')");

    // redirecting to success screen

    header("Location: thankyou.php");




Add New Contact

Add New Contact

<?php echo $err==''?'':('


') ?>
Contact Name:


Sure, the code above is not perfect. You can add more validation (i.e. check if email entered is valid), you can also make your code better structured, etc. In this code I also relied on php setting magic_quotes_gpc = Off (i.e. I am using addslashes() function to escape special chars). And, by the way, it is good idea to use mysql_real_escape_string() instead of addslashes() for mysql_query().
I have added some comments, so I think the code is self explanatory. But feel free to ask if you have any questions.

Thank you for the help.

I have been able to create the form, and just about everything has been working right, except the date. One of the things that we have to collect is the patients birthday. I have searched all over the internet, and have found various examples on getting drop down menus with the date, but no exact instructions on how to use the code.

Try jQuery datepicker for date field in your form.

Thank you, but I dont think that is not going to work. We need something quick and easy for the ladies filling out the forms. They need to be able to cycle through dozens of forms daily.

How about a way to combine the 3 date forms together? I have a year column, month a date. When these three drop down menus are selected, could I have another box that would autofill?

Sponsor our Newsletter | Privacy Policy | Terms of Service