Close

Form Snippets

How to Insert Form Data Into a MySQL Database Using PHP

Why won’t my form data insert properly into my MySQL database? Here’s the most common reasons why:

Get the updated source code for this video here.

The two most common issues I see developers having are:

  1. Not connecting to MySQL properly
  2. Not matching form fields to database columns properly

Issues Connecting to MySQL

When you run into connection issues, you’ll usually see an error that will say there was a problem connecting to the database.

More often than not, it’s simply a typo in your database name, username, or password.

Make sure to triple-check that information. Also, keep in mind if you’re using source code I’ve provide you’ll need to change those values to whatever they are for YOUR database.

Issues Matching Form Fields to Database Columns

Almost always, this is caused by a mis-match between your form field names and your database column names.

In your SQL statement, you do the matching directly. Here’s a quick example:

INSERT INTO table_name (database_column_1, database_column_2) VALUES (form_field_1, form_field_2)

So you need to match the form field value to the database column.

I go much more in-depth with this in the video, so be sure to watch it if you’re having this problem.

If you get value from this code snippet, please consider sharing it with another developer or group who could benefit from it.

April 29, 2015

How to Validate an Email Address (By Domain) Using PHP

How can I validate an email address (by domain) using PHP? Here’s how:

Here’s the class that does all the heavy lifting and an example page for including and using the class:

<?php
/**
* Validates email addresses
*
* This class is used to validate email addresses using several different criteria.
*
* @version 1.0
* @author John Morris <support@johnmorrisonline.com>
*/
class validate_email {
/**
* Accepted domains
* @access private
* @var array
*/
private $accepted_domains;

/**
* The class constructor
*
* This sets up the class
*/
public function __construct() {
// Set the accepted domains property
$this->accepted_domains = array(
'unl.edu', 'uni.edu'
);
}

/**
* Validate email address by domain
*
* Checks if the email address belongs to an accepted domain
*/
public function validate_by_domain($email_address) {
// Get the domain from the email address
$domain = $this->get_domain( trim( $email_address ) );

// Check if domain is accepted. Return return if so
if ( in_array( $domain, $this->accepted_domains ) ) {
return true;
}

return false;
}

/**
* Get the domain
*
* Gets the domain from an email address
*/
private function get_domain($email_address) {
// Check if a valid email address was submitted
if ( ! $this->is_email( $email_address ) ) {
return false;
}

// Split the email address at the @ symbol
$email_parts = explode( '@', $email_address );

// Pop off everything after the @ symbol
$domain = array_pop( $email_parts );

return $domain;
}

/**
* Check email address
*
* Checks if the submitted value is a valid email address
*/
private function is_email($email_address) {
// Filter submitted value to see if it's a proper email address
if ( filter_var ( $email_address, FILTER_VALIDATE_EMAIL ) ) {
return true;
}

return false;
}
}

<?php
require_once( dirname( __FILE__ ) . '/class-validate-email.php' );

$validate_email = new validate_email;

// Valid
if ( $validate_email->validate_by_domain('justin@unl.edu ') ) {
echo '<p>This is valid!</p>';
} else {
echo '<p>This is NOT valid!</p>';
}

// Valid
if ( $validate_email->validate_by_domain('jason@uni.edu') ) {
echo '<p>This is valid!</p>';
} else {
echo '<p>This is NOT valid!</p>';
}

// Not valid
if ( $validate_email->validate_by_domain('ramone@ui.edu') ) {
echo '<p>This is valid!</p>';
} else {
echo '<p>This is NOT valid!</p>';
}

These are the exact steps for using this:

  1. Include/require class-validate-email.php into your application or web page.
  2. Instantiate a new instance of the validate_email class.
  3. Pass the email address to the validate_email::validate_by_domain() method.

If you get value from this code snippet, please consider sharing it with another developer or group who could benefit from it.

April 28, 2015

PHP Multiple Checkbox Array Handling

Handling Checkboxes in PHP Forms

I tend to get a lot of questions around checkbox handling when submitting forms to  PHP. Here’s a simple snippet to illustrate how it works:

<?php
print_r($_POST);
?>
<form method="post">
Car <input type="checkbox" name="stuff[car]" value="1" />
Dog <input type="checkbox" name="stuff[dog]" value="1" />
<input type="submit" value="Submit" />
</form>

A couple things to pay attention to here…

1. Notice the input names. They follow this pattern name[key]. The name is really the name of the “group” of checkboxes. The key is the name of that individual checkbox. This is how you know which checkboxes were checked and which weren’t. When submitted, this form will only include the checkboxes that ARE checked.

2. Notice the created array. When this form is submitted the output will look something like this:

Array ( [stuff] => Array ( [car] => 1 [dog] => 1 ) )

If you look at our inputs… “stuff” becomes the top-level array key, “dog” and “car” become the 2nd-level array keys, and the input values become the values of “dog” and “car” respectively.

With this, you can properly handle these checkboxes and process them accordingly.

December 31, 2013