Posting information from a form on your website into a MySQL database is a pretty basic thing, and it is one of the most common functions of any website. I’ve had to re-create indentical scripts over and over to accomplish this simple task. So with saving time in mind, I finally just built a ‘flexible’ or ’scalable’ method of posting information to the database right from the form. The idea here is to create some code that will work when I add and remove form fields and not have any effect on the PHP portion of the page. I’ll just get right to it…
All of this would be in a file called addMember.php
Super simple form HTML:
———————————————–
<form action=”addMember.php” method=”post”>
<table border=”0″>
<tbody>
<tr>
<td>First Name</td>
<td><input name=”memberFirstName” size=”30″ type=”text” /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input name=”memberLastName” size=”30″ type=”text” /></td>
</tr>
<tr>
<td>Phone Number</td>
<td><input name=”memberPhone” size=”30″ type=”text” /></td>
</tr>
<tr>
<td>Email</td>
<td><input name=”memberEmail” size=”30″ type=”text” /></td>
</tr>
<tr>
<td></td>
<td><input type=”submit” name=”submitMember” value=”Add” /></td>
</tr></tbody></table>
</form>
———————————————–
Thats it for the form field’s HTML. Just a couple of basic fields to collect contact data about a person. Now rather than creating some PHP to specifically map these form fields to the database column names, I am going to do something a bit different. Check it out:
———————————————–
<?
//This is the code to be called when the form is submitted:
//if the form has been submitted
if(isset($_POST['submitMember']))
{
$keys = array(); //array for form keys
$values = array();//array for the data
$add = array_map(’mysql_real_escape_string’,$_POST); //safety first
//loop through each form field submitted in the $_POST array
foreach($add as $k => $v)
{
//if it is prefixed with ‘member’ it needs to be inserted to the database
if(ereg(’member’,$k))
{
$keys[] = str_replace(’member’,”,$k);//store the key (form field without ‘member’ prefix)
$values[] = “‘”.$v.”‘”; //store the corresponding data
}//if member
}//for each
//if atleast 1 form field has a ‘member’ prefix, store them to the database
if(count($keys)>1)
{
$key = implode(’,',$keys);//comma separate
$value = implode(’,',$values);//comma serparate
$sql = “INSERT INTO `user` (”.$key.”) VALUES (”.$value.”)”;
mysql_query($sql) or die(’Problem Adding User: ‘ . mysql_error() );
$msg = ‘<b>User successfully added to the database</b><br />’;
}//if count
}//if form submitted
?>
———————————————–
Note: I did have this tabbed out very neatly, but it didn’t appear in the post that way after I saved it, sorry.
What I did here is build an INSERT statement using the form field values as the table column name values, well almost. I prefixed the name of form fields with ‘member’ if I want it to be inserted to the database. This allows us to have form fields in the same form that don’t have to be inserted to the database, but can be used for anything else. The main key here is to match up your column names in the database to the form field names on your form. So a column in the database named “Address” would need to have a form field name of “memberAddress” to correctly line up when it comes time to input the data from the form to the database. This is likely a commonly used method of storing data, but I’ve seen plenty of times where it is not. This little trick will likely save you a lot of time down the road with your form submission scripts. You can almost copy and paste to use it over and over. The only real changes to be made are your table names and some form field names.
And that is it. Nothing too complex here, just a little technique I came up with and thought I’d share!