Saving form data to a MySQL database

Quform includes code to save form data to a MySQL database. To use this feature, open process.php and search for the term $config['database'] and change it to true. Then search for the term MySQL and you should end up above the block of code that does the saving to the database, you’ll need to change a few settings here to get it working.

Step 1 – Set connection details

Change the line that connects to MySQL with your correct host, username, password and database.

1
$db = new mysqli('localhost', 'username', 'password', 'database');
$db = new mysqli('localhost', 'username', 'password', 'database');

Step 2 – Set the database table

On the line below that you should change the table name to match your table name (inside the quotes).

1
$table = 'table';
$table = 'table';

Step 3 – Configure the data to save

Finally, configure the $data array to set which fields you want to save into what column. The left hand side of the array is the column name and the right hand side is the value to save. You can add new fields or remove fields to suit your needs, just duplicate a line inside the array and change the values to save to a new column.

1
2
3
4
5
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
);
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
);

The data in this array is escaped using $db->real_escape_string() before insertion to prevent SQL Injection attacks.

Accessing form data

Some types of fields such as Date and Multiple Checkbox return an array and will need additional processing before they can be inserted into the database, see the page Getting form values & data types and the Tips section below for more information.

Tips

Insert Date element value to DATETIME column

Before the $data array, add this line, if the element unique name is not ‘date’, change it below:

1
$date = $form->getValue('date');
$date = $form->getValue('date');

Add the line 5 below to the $data array, if the database column name in the database is not ‘date’ change that part.

1
2
3
4
56
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
    'date' => $date['year'] . '-' . $date['month'] . '-' . $date['day'],);
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
    'date' => $date['year'] . '-' . $date['month'] . '-' . $date['day'],
);

Insert Multiple Checkbox element value

Add the line 6 below to the $data array, if the database column name and element unique name are not ‘multi_checkbox’ change those parts (the column name is on the left and element unique name on the right of line 5). The values will be converted to a comma separated string.

1
2
3
4
56
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
    'multi_checkbox' => $form->getValuePlain('multi_checkbox'),);
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
    'multi_checkbox' => $form->getValuePlain('multi_checkbox'),
);

Insert a File Upload URL

Before the $data array, add this line, if the element unique name is not ‘upload’, change the that part on line 1:

12
$upload = $form->getValue('upload');$uploadUrl = isset($upload[0]['url']) ? $upload[0]['url'] : '';
$upload = $form->getValue('upload');
$uploadUrl = isset($upload[0]['url']) ? $upload[0]['url'] : '';

Add the line 5 line below to the $data array, if the database column name is not ‘upload_url’ change that part.

1
2
3
4
56
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
    'upload_url' => $uploadUrl,);
$data = array(
    'name' => $form->getValue('name'),
    'email' => $form->getValue('email'),
    'message' => $form->getValue('message'),
    'upload_url' => $uploadUrl,
);

Using a MySQL function as one of the column values

Change the query line to add your column name and function after the $columns and $values variables respectively e.g.

1
$query = "INSERT INTO `$table` ($columns, created_at) VALUES ('$values', NOW())";
$query = "INSERT INTO `$table` ($columns, created_at) VALUES ('$values', NOW())";

Still having trouble? Head over to the forums.

Forums