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

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

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.

See all 3 photos
Training Kit (Exam 70-463): Implementing a Data Warehouse With Microsoft SQL…
Current Bid: $21.99
Training Kit (Exam 70-463): Implementing a Data Warehouse With Microsoft SQL…
Current Bid: $23.50

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:

$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

Voter’s Information
First Name:
Middle Name:
Last Name:
Phone Number:

Sample Output:

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

Related PHP Tutorials and Sample PHP Scripts:

How to become a PHP Developer: Learn and Earn
How to run PHP Code or PHP Scripts and See the Result on Your Browser
PHP Tutorial: A Beginner’s Guide on PHP Programming-Part 1
PHP Tutorial: A Beginners Guide on PHP Programming-Part 2
PHP Tutorial: A Beginners Guide on PHP Programming Part 3
PHP code Example for if statement
PHP Code Example for if-else Statement With Notepad++
PHP Code Example on if else-if Statement
PHP Code Example on Switch Statement:Making Choice and Cases
PHP Code Example on While Loop
PHP Code Example on Numeric Array
PHP Code Example on Do-while Loop
PHP Code Example on Foreach Loop and For Loop
PHP Code Example on Associative Array and Multidimensional Array
PHP Sample Codes On Function:How to make Function in PHP