Q:

SQLite update is replacing fields with null values

Okay so ive been reading up on and working with SQLite recently (someone on here suggested it actually).
It works great for what I need - an in-memory database. The problem I am having however is that their are duplicate records in here so what I wanted to do was insert the record but if it already exists then just fill in the missing fields.
Now the first method I tried was setting the id as a primary key and INSERT OR REPLACE into. The issue with this was the previous contents were being wiped out.
So what im doing now is an update query. Im them checking the number of rows changed. if its below 1 then I run the insert query (if there a better way do share as I know this has extra overhead).
Anyway my issue (finally).. is that even with the update query the records are being overwritten with null values.
Ive condensed the snippets of code below:
    $stmt1 = $db->prepare("UPDATE results SET 
        field1=?, field2=?, field3=? 
        WHERE id=?
    ");

    $stmt1->execute(array(
        $elm['content1'], $elm['content2'], $elm['content3'], $elm['id']
    ));

    $count = $stmt1->rowCount();

    if ($count < 1) {
        $stmt2 = $db->prepare("INSERT INTO results (id, field1, field2, field3) 
            VALUES (:id,:field1, :field1, :field1 )
        ");

        $stmt2->execute(array(":id" => $recordID, ":field1" => $elm['content1'], ":field2" => $elm['content2'], ":field3" => $elm['content3']));    
    }

The above is going on within a foreach loop.
Is their anyway to stop the content being overwritten if its already in the db. So if on the update the field is null then add the new content, if its already got something in it leave it unaltered and move on to the next field. Ive read about an IFNULL which can be used in SQLite but im not sure how I use that within my PDO prepared statements. 
Any help, guidance, examples would be appreciated :)
p.s Im using PHP5 with SQLite

A:

if on the update the field is null then add the new content, if its already got something in it leave it unaltered and move on to the next field.
You can use coalesce to do this; here is an example:
~ e$ sqlite3
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo (a,b,c);
sqlite> insert into foo values (1,NULL,3);
sqlite> select * from foo;
1||3
sqlite> update foo set a = a + 1, b = coalesce(b,'b'), c = coalesce(c,'c');
sqlite> select * from foo;
2|b|3
sqlite> 

So, your stmt1 would be:
$stmt1 = $db->prepare("UPDATE results SET 
    field1=coalesce(field1,?), field2=coalesce(field2,?), field3=coalesce(field3,?)
    WHERE id=?
");

