Job2 Street

Thursday, December 4, 2014

How to store your web or online form data into a MySQL database using PHP

How to store your web or online form data into a MySQL database using PHP


You can either store your web or online form data into a database using PHP — MySQL or some other — or you can get it mailed to you. Read how to connect to MySQL database with PHP. It’s preferable to mail the data to you each time someone fills up your web form but for later analysis it’s advised that you also store it somewhere, especially when you are gathering information like age, gender and the reason of visiting your website.
Although there is a multitude of databases and languages available to you, most web hosts provide PHP and MySQL so we’ll base our example on this prevalent database server. We’ll first create an online form that accept the following information:
  • Name
  • Age
  • Gender
  • Country
To store this information into your MySQL database either should already be having a table that can accommodate this data, or you should create it. In your hosting account (or local host if you have installed the MySQL database on your PC) you can easily go to phpmyadmin and create a table “mydata” (you can have any name):
CREATE TABLE mydata (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
age integer,
gender VHARCHAR(20),
country VARCHAR(255),
PRIMARY KEY (id));
And now your table is all ready to store your web form data.
The code for the form goes like this:
<form name=”user_details” id=”user_details” method=”post” action=”save-in-database.php”>
        <p><label>Enter Name:</label> <input type=”text” name=”name” id=”name” /></p>
        <p><label>Enter Age:</label> <input type=”text” name=”age” id=”age” /></p>
        <p><label>Enter Gender:</label> <input type=”text” name=”gender” id=”gender” /></p>
        <p><label>Enter Country:</label> <input type=”text” name=”country” id=”country” /></p>
        <p><input type=”submit” name=”s1″ value=”Submit” /> <input type=”reset” name=”r1″ value=”Reset” /></p>
</form>
We could have jazzed it up a bit more with formatting the date field and using a dropdown for countries, but right now our main purpose is to know how to store your online form data into a MySQL database. As you can see above, the action attribute refers to the file “save-in-database.php” and this is the file that receives the information from the form and saves it.
First, let is store the form details in local PHP variables:
<?php
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['age'];
$country = $_POST['country'];
?>
We use POST because in the method attribute of the form we specified “post”. You can use either “post” or “get”. Once you have stored the form data in the variables you need to connect to the MySQL database server and we’re going to borrow the code from the blog post link mentioned above:
<?php
$host=”mysql.yourdomain.com”;
$user_name=”peter”;
$pwd=”F$5tBGtr”;
$database_name=”our_clients”;
$db=mysql_connect($host, $user_name, $pwd);
if (mysql_error() > “”) print mysql_error() . “<br>”;
mysql_select_db($database_name, $db);
if (mysql_error() > “”) print mysql_error() . “<br>”;
?>
Once we have successfully established the MySQL database connection, we’ll use PHP’s inbuilt MySQL functions to store the values into the database table “mydata”:
<?php
$query = “insert into mydata (name, age, gender, country) values (‘” . $name . “‘, ” . $age . “, ‘” . $gender . “‘, ‘” . $country . “‘)”;
$qresult = mysql_query($query);
?>
You create a simple MySQL command, store it in a string and then execute it by using the PHP function mysql_query(). There are many validations you can carry out to improve and maintain data integrity, but this is a simple example of storing your online form data in MySQL database using PHP.

 Related posts

http://www.howtoplaza.com/store-online-form-data-into-mysql-database-using-php

Alternatively, you can run a MySQL query, which will create the table. The format is:
CREATE TABLE tablename (Fields)
The fields are defined as follows:
fieldname type(length) extra info,
The fields are separated by comma.
For example, if you wish to create a table called Members with 3 fields in it - FirstName, LastName and Age, you should execute the following query:
CREATE TABLE Members(FirstName varchar(15),LastName varchar(15),Age int);

No comments:

Post a Comment

K-Play

kPlaylist - get your music online

The player will show in this paragraph

2 3

IkimFM

speaker music speaker