Saturday, December 7

Form to MySQL Database

Building web applications in php can be extremely useful. PHP is fast and easy. Even if you know a very little about it you can create fascinating web application with the help of database connectivity. The most important task of which could be adding data submitted through the form to a database (here MySQL).
In this post we will see a sample of code which could add data to database.
We will need the following things -
  • MySQL server
  • PHP server
  • Any Text editor

I will explain this from the beginning, right from creating a MySQL database, adding tables to database and sample form.

MySQL Section
Connect to MySQL console using the root user and password, run the following commands to create a database and add tables to it.

create database users;
use users;
create table userdetails (name VARCHAR(30),emailid VARCHAR(30),address VARCHAR(255),comments VARCHAR(255));

The first line of the above code will create a database with name users (you can customize this). The second line will use that database, and third will create a table in that database with name, emailid, address and comments as four fields.

PHP Section

For php we will simplify the process of connecting to database and adding content in 3 different parts. config.php (contains the connectivity credentials) and opendb.php (establishes the connection to MySQL server), and add.php (will add the form content to database)

config.php (change content accordingly)

<?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'users'; ?>

opendb.php

<?php $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); ?> 

add.php

<?php
include 'config.php';
include 'opendb.php';

$sql="INSERT INTO userdetails (name, emailid, address, comments)
VALUES
('$_POST[name]','$_POST[email]','$_POST[addr]','$_POST[comm]')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
echo "Records added successfully";
mysql_close($conn);
?>

The first two lines of the code will call the config.php file and opendb.php. After connection to the server is established the $sql will instruct the server to add the content received from the form to the database in the specified fields. We will use the POST method to receive the content from the user. The last line of the code will disconnect from the MySQL database (We can remove that line as it is not mandatory).

The sample submit.html is given below.

<html>
<body>
<form action="add.php" method="post"> 
<table style="width: 100%;" border="0">
<tbody>
<tr>
<td width="22%">Name</td>
<td width="1%">:</td>
<td width="77%"><input name="name" type="text" size="30" /></td>
</tr>
<tr>
<td>Email Id</td>
<td>:</td>
<td><input name="email" type="text" size="30" /></td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><textarea name="addr" cols="23" rows="3"></textarea></td>
</tr>
<tr>
  <td>Comments</td>
  <td>:</td>
  <td><textarea name="comm" cols="23" rows="4"></textarea></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td><input name="submit" type="submit" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

In this way we can add data directly to database and can retrieve or process it later.

No comments:

Post a Comment

Comment anything you want. Just be polite and give respect to others!
I am simply going to remove the comments which are offensive or are off topic.
And please don't spam your website links in comments. I don't, neither should you.