Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Save image as BLOB with SQLite plugin

form plugins

Started by DouxDoux 2 years ago · 1 replies · 293 views
2 years ago

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):

PHP
        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:

PHP
$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.

2 years ago

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:

PHP
$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

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1133 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 131 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 107 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 125 7 months ago