PHP / MYSQL:仅在非空逗号时更新字段

I have been searching for an elegant, efficient way to update fields only if they have a value but have not found one. So I decided to bite the bullet and build a query using use if/then statements. After doing this, however I got an error having to do with an extra comma in the SQL statement and now I'm thinking it may be necessary to conditionally insert commas after the field which adds a another level of complexity

Am I correct that you cannot have a comma before the WHERE statement.

Also, is there in fact, no elegant way to check for empty fields prior to updating table?

   $sql = "UPDATE items SET ";
    if ($name<>'') {
    $sql.="name = '$name', ";
    }
    if ($color<>'') {
    $sql.="color='$color', ";
    }
    if ($quantity<>'') {
    $sql .="quantity='$quantity', ";
    }
    if ($size<>'') {
    $sql.="size='$size', ";
    }
    if ($pic <> '') {
    $sql.="pic = '$pic'";
    }
    $sql.= "WHERE id = $itemid && custid=$custid";

    /*offending comma after 'large' ->  'size= 'large', WHERE itemid=2231 && custid = 221
    */

Thanks for any suggestions.

If $pic of the condition fails then you get comma at last position so you need to use rtrim() before where condition

$sql = "UPDATE items SET ";
    if ($name<>'') {
    $sql.="name = '$name', ";
    }
    if ($color<>'') {
    $sql.="color='$color', ";
    }
    if ($quantity<>'') {
    $sql .="quantity='$quantity', ";
    }
    if ($size<>'') {
    $sql.="size='$size', ";
    }
    if ($pic <> '') {
    $sql.="pic = '$pic'";
    }

 $sql=rtrim($sql, ", ");// it will remove last comma and space
 $sql.= " WHERE id = $itemid && custid=$custid";// add space before where