Save image as BLOB with SQLite plugin

I’m using both the form plugin and the SQLite plugin to save every submission in a SQLite db. Everything works fine, except it’s not letting me save images in my db. I’m new to php so it might be something obvious.

So this seems to be the piece of code I should modify (the complete code is on this github):

        switch ($action) {
            case 'sql-insert':
                  $data = $form->value()->toArray();
                  if ( isset($params['ignore'])) {
                      foreach ( $params['ignore'] as $k ) unset( $data[$k] );
                  }
                  $fields = '';
                  $values = '';
                  $nxt = false;
                  foreach ( $data as $field => $value ) {
                    // remove fields associated with Form plugins
                    if ( preg_match('/^\\_|form\\-nonce/', $field ) ) continue; // next iteration if error (false) in match, or match succeeds.
                    $fields .= ( $nxt ? ',' : '') . $field;
                    $values .= ( $nxt ? ',' : '' ) . '"' . $value . '"' ;
                    $nxt = true;
                  }
                  if (isset($data['where'])) {
                      unset($data['where']); // dont want it polluting UPDATE as a field. Should be ignored
                  }
                  $set = 'SET ';
                  $nxt = false;
                  foreach ( $data as $field => $value ) {
                        $set .= ( $nxt ? ', ' : '') ;
                        $set .= $field . '="' . $value . '"' ;
                        $nxt = true;
                  }
                  $sql ="INSERT INTO {$params['table']} ( $fields ) VALUES ( $values )";
                  $this->log(self::INSERT,$sql);
                  $db = $this->grav['sqlite']['db'];

This is the error message I get: "Array to string conversion" which seems to be related to this line: $values .= ( $nxt ? ',' : '' ) . '"' . $value . '"' ;

I’ve tried to modify it a little bit, but keep getting the same error. Here’s what I did:

$fields = '';
                  $values = '';
                  $nxt = false;
                  foreach ( $data as $field => $value ) {
                    if ( preg_match('/^\\_|form\\-nonce/', $field ) ) continue; 
                    $fields .= ( $nxt ? ',' : '') . $field;
                    try{
                      $values .= ( $nxt ? ',' : '' ) . '"' . $value . '"' ;
                    } catch(Exception $e){ // I've added a try-catch to deal with the error so if the value is an image, it should be read as BLOB
                      $values .= file_get_contents( $nxt ? ',' : '' ) . '"' . $value . '"' ;
                    }
                    $nxt = true;
                  }

Thanks in advance.

A much simpler and safer way to build a comma-delimited string from an array is to add new members to an array in your loop rather than attempting to concatenate. Then use PHP’s implode function to join them with a comma. Something like:

$fields_array = array();
foreach($data as $field => $value) {
  $fields_array[] = $field;
}
$fields = implode(',', $fields_array);

There are other ways, but that should take care of a lot of that complicated logic for you.

You can also simplify expressions like '"' . $value . '"' using string interpolation and escaping the quotes, ending up with something like: "\"$value\"". It’s a matter of taste though.

1 Like