post your comment   print   send to a friend
Rate:  70% | Views: 16058
Question categories:  MySQL

How can i send form data to a mysql database using php?

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("(database_name).mysql.aplus.net","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.

Customer Feedback
Rate:  70% | Views: 16058 | Please Rate:  
 
If you have other comments or ideas for future technical tips, please type them here:

Email: (optional)

Comments: (optional)

 Domain Name Registration | Ecommerce Web Hosting    Back to serch results
Browse the Base
Knowledge Base
Shared Hosting
  Unix Plans
    MySQL
Messages
Private Area
 
Ask
in Private
   
Personal
Folder
 
Related Questions
 
1. How to set up an additional MySQL Database?
 
2. How do I get MySQL access?
 
3. What is the size limit for MySql Databases?
 
4. Where can I get a good MySQL authoring tool?
 
5. What is the size limit (in Megabytes) for MySql Databases under the plans that support MySQl Databases?
 
Home Browse Search Ask in Private Personal Folder   Help
powered by web hosting 
  Logged as: Guest