PHP/MYSQL UPDATE only if value is not null

forms, mysql, php

Solution

You can use `COALESCE`

       UPDATE table
         SET a = COALESCE($1, a),
             b = COALESCE($2, b),
             c = COALESCE($3, c)
         WHERE row = ''

Problem

I'm building a form to update multiple columns of data. This code has been partially successful for my purposes. The only thing left to do is include (if IS NOT NULL) for the update query. I'm not sure how to go about this. In other words, I only want the UPDATE to execute if the $_POST value is not empty. ``` <form> <input type='text' name='input1' /> <input type='text' name='input2' /> <input type='text' name='input3' /> <input type='submit' value='submit' /> </form> <?php //db connect $1=$_POST['input1']; $2=$_POST['input2']; $3=$_POST['input3']; mysql_query("UPDATE table SET a = $1 b = $2 c = $3 WHERE row = 'row_id"); ); ?> ``` Thanks in advance for the help. (To save you from some extra typing, my original code escapes characters so warnings of SQL injections aren't necessary. I'm also in the process of familiarizing myself with "mysqli_query", so no need to comment on that either.)

Original source