KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Insert form data into MySQL using php

Before you start this lesson, I assume that you finished reading the following tutorials:

  1. install phpmyAdmin, mysql , php
  2. Create Database by using phpMyAdmin
  3. Working with forms

In this lesson, we will learn how to insert data collected in form into MySQL database.

In Working with forms, we already know how to get the form input. We need add a little bit more code to insert data into Mysql Database.

PHP Connect to Mysql Database

The PHP code connecting to MySql database is following:

$username="root";
$password="YourPassword";
$database="phonebook";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

In the above code, we create 3 variables, $username,$password and $database, which store the user name, database name and your password.

mysql_connect(<address>, <username>, <password>) connect to your local MySQL.In our example, <address> is localhost or may be a remote machine IP address. @mysql_select_db($database) specifies which database in MySQL you are gonna use.Placing a @ symbol in front of the function name tells the function to fail silently, allowing us to display our own, friendlier error message. die(...) just show an error message in case connection to database fails and terminate the script execution.

Php insert data into Mysql

PHP uses the following statements to insert data into MySQL.

$name=$_POST['name'];
$phone=$_POST['phone'];
$query = "INSERT INTO contact VALUES ('$name','$phone')";
mysql_query($query) or die('Error, insert query failed');

In the above code, $name and $phone are form input from user. $query is a standard SQL insert query, which syntax is

insert into table-name values ('','',''...);

mysql_query($query) executes the query. die(...) will print out error message and end the script running if query fails.

Overall code of PHP insert data into MySQL.

<html>
<body>
<?php
$username="root";
$password="123456";
$database="phonebook";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
$name=$_POST['name'];
$phone=$_POST['phone'];
$query = "INSERT INTO contact VALUES ('$name','$phone')";
mysql_query($query) or die('Error, insert query failed');
echo "Hi! $name. Your phone number <b> $phone </b> is in our database";
?>
</body>
</html>

Save the above code to a file named action.php. Input this file name in your form tag "action" field. Your form may look like this:

<html>
<body>
<form action="action.php" method=post>
<input name="name" type=text ><br>
<input name="phone" type=text ><br>
<input type="submit" >
</form>
</body>
</html>

Check PHP insert result by PHPMyAdmin


You may check if your input data in database or not by using PhPMyadmin. Launch your phpMyAdmin, select your database (phonebook in this lesson) and click on the table "contact". Then, on the right side you will see the following picture:

php tutorial,mysql,phpmyadmin,create database

Suppose you input "Tom" as name, "123456789" as phone number. Click on the browser, you will see the following :

php tutorial,mysql,phpmyadmin,create database

Did you see your input in the above image?

Congratulations! You did it.

PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
Set cookie and delete cookie
Most often used MySQL functions

 

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer