Format a php array for MySQL insert

Hello Board,
I need some help getting data in an array formatted so I can insert it into a MySQL database.
I am using a tool called PDFtk to dump the labels and values of a PDF form. My goal is to insert the data from that form into a MySQL database.
PDFtk doesn’t have too many options for the command line interface that I am using.
The array I have now looks like what you see below. The array represents 1 record in my MySQL table. The MySQL column names are the values after FieldName (‘customer’, ‘license’, ‘lic_class’…etc.)…and the values for that column are the next item in the array, FieldValue. I hope that makes sense…
My question is : how do I take this array and create 1 record in my database with the values given.
Thanks,
Kelly

array(1047) {
[0]=>
string(3) “—”
[1]=>
string(19) “FieldName: customer”
[2]=>
string(33) “FieldValue: Jon Smith / Principal”
[3]=>
string(3) “—”
[4]=>
string(18) “FieldName: license”
[5]=>
string(33) “FieldValue: 00000001 / 2019-06-30”
[6]=>
string(3) “—”
[7]=>
string(20) “FieldName: lic_class”
[8]=>
string(26) “FieldValue: A, C10 & B”
[9]=>
string(3) “—”
[10]=>
string(23) “FieldName: name_of_firm”
[11]=>
string(32) “FieldValue: ABC Contractor, Inc.”
[12]=>
string(3) “—”
[13]=>
string(18) “FieldName: address”
[14]=>
string(3) “—”

Something like this?

<?php

function convertArray($array)
{
    $name = '';
    $result = [];
                  
    foreach($array as $value ){
        if(substr($value, 0, 9) == 'FieldName') {
            $name = substr($value, 11);
        } elseif(substr($value, 0, 10) == 'FieldValue') {
            $result[$name] = substr($value, 12);
        }
    }
    
    return $result;
}

$array = array(
    '—',
    'FieldName: customer',
    'FieldValue: Jon Smith / Principal',
    '—',
    'FieldName: license',
    'FieldValue: 00000001 / 2019-06-30',
);


print_r(convertArray($array));

Result:

Array
(
    [customer] => Jon Smith / Principal
    [license] => 00000001 / 2019-06-30
)

Frankbeen,
Thank you so much, that works beautifully!

I’m grinding out a solution for this project, which is to take a user filled out PDF form (created by tcpdf) and import the data into my database. However, to get to the point I’m at now, I’m using another utility called PDFtk, which I run with a PHP exec command. Are you aware of any pure PHP solution to what I want to do? TCPDF doesn’t seem to have a function to interpret PDFs, only generate them. The PHP library itself seems pretty dates…thanks again!

1 Like

Depends how complex you want to get. OCR is the most complex.

For other possibilities, there is this library, https://pdfparser.org/

I looked at PDFParser the other day and tried the demo. It didn’t work too good on converting the PDF I uploaded. It didn’t recognize a lot of the form data. I don’t have to worry about OCR as I create the PDFs with TCPDF. PDFtk toolkit works great, and there is a PHP implementation at github, but it didn’t work as well as the command line tool…thanks!

So you are adding the values that you want parsed?

I have created a PDF using TCPDF that includes form fields for my clients to enter.
Up until now, I have been receiving the filled out PDF back from my clientsand entering the data in manually.
My goal is to automate the insert of the data into my database.

I have it working now using a command line tool PDFtk toolkit, using PHP Exec command. I’d rather do it purely with PHP but have I tried various PHP PDF libraries and couldn’t get anywhere, mainly from my lack of experience.

Are these PDF’s emailed back to you and then parsed or are they input from a site?

Currently, they are emailed back to me.

Walk me through the process then. There may be other ways do deal with this problem.

I have created a PDF using TCPDF that includes about 300 entry fields.
I email this to my new clients to fill out, and to my existing clients once a year for updating.
It includes information like names, addresses, their subcontractors, etc.
Currently, after they fill it out they email it back to me and I enter the information in my MySQL database.
Very time consuming and prone to entry errors, so I want to automate this input. I have thought about creating a website and allowing clients to logon and enter the information directly, but I don’t want the hassles with user names, passwords, and security. Plus, some of my clients are old school and not as comfortable with this.

Thanks for any suggestions you can provide!

Would they upload the form? Or strictly return the email with attachment?

My thought process was that they would email it. But if you have an idea around the PDF being uploaded, I am all ears…thanks!

You could use that same tool from an upload script, combined with the array formatting that @frankbeen worked out, and automate the process a bit.

The PDFtk tool is a windows binary .exe file. Is it possible to put that on my webserver and execute it on the client? (without putting it on every client PC).

No that won’t work. Browsers builds security shields around themselves to protect their users and their data.

It’s also available on Linux, https://www.pdflabs.com/docs/install-pdftk-on-redhat-or-centos/

So you can set a process that would fun the form for processing after validation.

Thanks so much for your help! I have successfully installed and run a PHP Exec PDFtk on my Centos 6 server. I appreciate yours and FrankBeen’s time.

2 Likes

Glad it’s working. The uploading of files would just streamline the process. If you need help let me know if it is a path you are thinking about. The other option, even more obscure, it to have a program watch an email and upload it on its own.

Sponsor our Newsletter | Privacy Policy | Terms of Service