Store XFDF User Information in a Database

This article assumes you have the following in place and tested:

Server preparation

There are a few things you will need before we can even begin. Firstly, you need some kind of database functionality available in PHP. The scope of this article does not go into detail for each type of system or specifics of them. Since every request I have gotten for information about this has been specific to MySQL databases, that is what I will use here. Specifically, the examples will show how to do this using the MySQL Improved Extension. For other systems, you will need to modify the scripts slightly to use the correct versions of the functions presented here.

Of course, in order to even use the functions that are available, you will need to have the login credentials for the database system you plan to use. You will need to know the hostname (or IP address), the username, the password and the database name. If you plan to use SQLite for your database, you only need worry about the file permissions.

In addition to having the ability to utilize function in PHP and being able to login to the databse server, you will also need a table structure. The structure used in this article is based on the same data as presented in the step for processing the submitted data:

CREATE TABLE `user_xfdf_data` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `submit_date` datetime NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `email` varchar(150) NOT NULL,
  `dob` date NOT NULL,
  `notes` text NOT NULL,
  PRIMARY KEY (`id`)
);

Connecting to the Database and Writing Data

Using the information stored in the $data array from our processing earlier, we can now insert the data into the database table we created above:

$mysqli = new mysqli( 'localhost', 'username', 'password', 'database_name' );
if( mysqli_connect_errno() )
{
    printf( "Connect failed: %s\n", mysqli_connect_error() );
    exit();
}
$mysqli->autocommit( TRUE );

$q = "INSERT INTO user_xfdf_data(
    submit_date, first_name, last_name, email, dob, notes
    ) VALUES (
    '" . $mysqli->real_escape_string( date( 'Y-m-d H:i:s' ) ) . "',
    '" . $mysqli->real_escape_string( $data['first_name'] ) . "',
    '" . $mysqli->real_escape_string( $data['last_name'] ) . "',
    '" . $mysqli->real_escape_string( $data['email'] ) . "',
    '" . $mysqli->real_escape_string( $data['dob'] ) . "',
    '" . $mysqli->real_escape_string( $data['notes'] ) . "'
    )";
$res = $mysqli->real_query($q);
if( !$res )
{
    printf( "Error: %s<br/>\n%s\n", mysqli_connect_error(), $q );
    exit();
}

If there aren't any errors, the submitted data has now been stored in the database.

Generating the XFDF From What is in the Database

You can use the information in the table to generate the XFDF formatted data on request by utilizing the createXFDF() function on queried data records:

$q = 'SELECT first_name, last_name, email, dob, notes
    FROM user_xfdf_data WHERE id = 1';
$res = $mysqli->query($q);
if( $res->num_rows )
{
    $data = $res->fetch_array( MYSQLI_ASSOC );
    $xfdf = createXFDF( $pdf_file_url, $data );
}

Once you have the XFDF stored in the $xfdf variable, you can utilize it however you like. Some suggestions can be found on the main article under the heading Additional Code Examples.

Suggestions or Improvements?

Do you have something that may benefit others regarding what you find on this page? Would you like to share it? If so, simply contact me and I will review it and possibly post it here.

Did this article help you save time for a project? Did this information help you create your website or web application? If so, please send a donation my way - no amount is too small to show your appreciation.