|
Answer
by: Stuart Pierce, Aplus.Net Knowledge Base Support
You
can use a PHP script to send data collected by an online form to your
MySQL database. In our example, two files are needed for this purpose:
- the
HTML online form where users' data will be collected (e.g.
formindb.html);
- and
the PHP script which will send this data to your MySQL database (e.g.
formindb.php).
I.
HTML online form file (formindb.html)
Here is the HTML code which creates an online form with two text fields
to be filled in by the user: Your Name and Email Address.
<html>
<head>
<title>Formmail
in DB</title>
</head>
<body>
<!-------------begin
form------------>
<FORM
ACTION="formindb.php" METHOD="POST" NAME="contact_form">
<TABLE>
<TR>
<TD><font size="2" face="Verdana, Arial,
Helvetica, sans-serif">Your
Name:</font></TD>
<TD> <input
type=text name="name"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana, Arial,
Helvetica, sans-serif">Email
Address:</font></TD>
<TD><input
type=text name="email"></TD>
</TR>
<TR>
<TD><input
type="submit" value="Submit" name="Submit"></TD>
<TD><input
type="reset" value="Reset" name="Reset"></TD>
</TR>
</TABLE>
</FORM>
<!-------------end
form------------>
</body>
</html>
II.
PHP scipt (formindb.php)
Here is the PHP scipt which will take the data input at the HTML file
and send it to your MySQL database. Please pay special attention to the
variables (in bold) which have to be modified.
<?php
$con =
mysql_connect("localhost","yourDatabaseUsername","yourDatabasePassword");
//Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not
connect: ' . mysql_error());
}
mysql_select_db("yourDatabaseName",
$con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This
value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same
as in the HTML form file
$sql="INSERT
INTO form_data
(name,email) VALUES
('$name','$email')"; /*form_data
is the name of the MySQL table where the form data
will be saved.
name and email are the
respective table fields*/
if
(!mysql_query($sql,$con)) {
die('Error:
' . mysql_error());
}
echo "The form
data was successfully added to your database.";
mysql_close($con);
?>
Important:
The MySQL
table and fields to store the form data (in our example: form_data,
name and email) should exist prior to using the form. The script will
not create them in your database. Here is how to create tables and
fields in MySQL using the phpMyAdmin authoring tool:
III.
Upload both formindb.html and formindb.php files to your html
directory or one of its subdirectories. If you upload to your main html
directory, the form will be available at
http://yourdomain.com/formindb.html, where "yourdomain.com" is your
actual hosted domain name.
According to the example above, users will be asked to enter a name and
email address, and then submit. You can modify the formindb.html file
to add additional input fields.
Note: The coding examples above are considered secure. However, we are
not responsible for any security issues they might lead to.
|