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.

Example:

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

Code:

CREATE DATABASE voters_information

2. Create the database table

Code:

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)

Code:

<?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 ());

mysql_select_db($db)

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

?>

PHP Scripts on Adding Information to the Database

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


<?php
 require 'config.php';
 
 if(isset($_POST[submit]))
 {
	$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',
		Phone_No='$Phone_No'");
		
	
	if($query)
	{
		echo "<script>alert('Successfully Saved to the database. Enter again?'); document.location.href='index.php';</script>";
	}else{
		echo "<script>alert('Failed to save to the database, Please add again'); 
		document.location.href='index.php';</script>";
	}
 }
?>
<html>

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

Comments

    0 of 8192 characters used
    Post Comment

    • 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 :/

    • aisha91 profile image
      Author

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

    Click to Rate This Article