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.