ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

SQL Tutorial: Add or Insert Information to My SQL Database with PHP Scripts

Updated on April 9, 2013

To add or insert information to the database always check if the html form and query coincide to the fields you have on your database, so that you can avoid confusion and numerous number of failed attempts on adding information to your database.

Steps on How to add or insert information to the database

1-3. Create your database, create database table and table fields, and configure your database. See example here.

4. Create html form that coincide to the fields of tables you have on your database

5. Create add.php that will be executed as the user clicks the submit button on your html form.


I would create a database with a database name ‘voters_information’ and a table name ‘voters_info’, the number of fields would be 5, the first name of the voter, middle name, last name, address and phone number.

Things you should do:

1. Create the database


CREATE DATABASE voters_information

2. Create the database table


Create table voters_info


Voters_id not null int primary key auto_increment,

Firstname text not null,

Middlename text not null,

Lastname text not null,

Address text not null,

Phone_No int not null


3. Connect the database (config.php)



$server = 'localhost';

$user = 'root';

$pass = "";

$db = 'voters_information';

// Connect to Database

$connection = mysql_connect($server, $user, $pass)

or die ("Could not connect to server ... \n" . mysql_error ());


or die ("Could not connect to database ... \n" . mysql_error ());


PHP Scripts on Adding Information to the Database

 <center><form method="post" action="">
            <legend>Voter's Information</legend>
                    <td>First Name:</td>
                    <td><input type = "text" name = "Firstname"/></td>
                    <td>Middle Name:</td>
                    <td><input type = "text" name = "Middlename"/></td>
                    <td>Last Name:</td>
                    <td><input type = "text" name = "Lastname"/></td>
                    <td>Phone Number:</td>
                    <td><input type = "password" name = "Phone_No"/></td>
			<td><input type="submit" name="submit" value="SUBMIT"/></td>

 require 'config.php';
	$Firstname = mysql_real_escape_string(htmlspecialchars($_POST['Firstname']));
	$Middlename = mysql_real_escape_string(htmlspecialchars($_POST['Middlename']));
	$Lastname = mysql_real_escape_string(htmlspecialchars($_POST['Lastname']));
	$Phone_No = mysql_real_escape_string(htmlspecialchars($_POST['Phone_No']));
	$query = mysql_query("INSERT INTO voters_info SET Firstname='$Firstname', Middlename='$Middlename', Lastname='$Lastname',
		echo "<script>alert('Successfully Saved to the database. Enter again?'); document.location.href='index.php';</script>";
		echo "<script>alert('Failed to save to the database, Please add again'); 

Sample Output:

Filled HTML Form
Filled HTML Form
After Clicking the Submit Button
After Clicking the Submit Button
After Clicking OK on the Alert Box
After Clicking OK on the Alert Box
Information Saved to the Database
Information Saved to the Database


    0 of 8192 characters used
    Post Comment

    • aisha91 profile image

      Rasna Aisha 4 years ago from Manila, Philippines

      Hi Spongyollama,

      You're right, especially if you have your own website. A little knowledge about html, css, javascript and php would help us a lot to redesign our own website according to our taste without spending money. Those free templates nowadays are always have something 'lacking' base on our needs. Good luck to your learning :)

    • Spongy0llama profile image

      Jake Brannen 4 years ago from Canada

      Thanks! I still struggle with website coding... I guess it's just a new language you have to learn these days :/