Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only update when value is not null mySQL

Tags:

php

mysql

I am working on an update query where the values should only update when the value is not null or empty. Now it updates everything regardless the value. Please help me out with this one.

$query = "UPDATE bundels 
SET batchkosten = CASE WHEN  ". $_POST['batchkosten'] . " IS NOT NULL
                   THEN  ". $_POST['batchkosten'] . "
                   ELSE batchkosten
              END CASE,
              CASE WHEN   ". $_POST['maandelijkse_kosten'] . "  IS NOT NULL
                   THEN   ". $_POST['maandelijkse_kosten'] . " 
                   ELSE maandelijkse_kosten
              END CASE,
              CASE WHEN   ". $_POST['aanmeldkosten'] . "  IS NOT NULL
                   THEN   ". $_POST['aanmeldkosten'] . " 
                   ELSE aanmeldkosten
              END CASE,
              CASE WHEN   ". $_POST['transactiekosten'] . "  IS NOT NULL
                   THEN   ". $_POST['transactiekosten'] . " 
                   ELSE transactiekosten
              END CASE,
              CASE WHEN   ". $_POST['referral'] . "  IS NOT NULL
                   THEN   ". $_POST['referral'] . " 
                   ELSE referral
              END CASE,
              CASE WHEN   ". $_POST['actief'] . "  IS NOT NULL
                   THEN   ". $_POST['actief'] . " 
                   ELSE actief
              END CASE
              WHERE bundel_id = ". $_POST['bundel_id'] . "";
        $result = mysql_query($query, $db) or die ('FOUT: werkt niet'); 
            header ("Location: vergelijker_bewerken.php");
        } else {
            $bundels = mysql_query("SELECT bundels.psp_id, psp.psp_id, psp_naam, bundels.bundel_id, batchkosten, maandelijkse_kosten, aanmeldkosten, transactiekosten, referral, actief from bundels
                                    JOIN psp
                                ON psp.psp_id = bundels.psp_ID"); 
}
like image 623
Broodje Avatar asked Dec 15 '14 12:12

Broodje


2 Answers

  1. Use Prepared Statements to escape user input and avoid SQL syntax errors and SQL injections.
  2. You can use a case

    UPDATE bundels 
    SET batchkosten = case when ? is not null and length(?) > 0
                           then ?
                           else batchkosten
                      end,
    ...
    

Your current query translates to (which should throw an error actually)

UPDATE bundels 
SET batchkosten = CASE WHEN ? length(?) > 0 
                       THEN ?
                       ELSE batchkosten 
                  END 
WHERE bundel_id = ? 

But use instead:

SET batchkosten = CASE WHEN ? is not null and length(?) > 0 
like image 153
juergen d Avatar answered Sep 30 '22 19:09

juergen d


you can write script some like this maybe:

$query = "Update bundels SET ";
$columns = array( "batchkosten", 
                  "maandelijkse_kosten", 
                  "aanmeldkosten",    
                  "transactiekosten", 
                  "referral", 
                  "actief");

foreach($columns as $column){
    if(isset($_POST[$column]) && !empty($_POST[$column])){
        $query .= $column . " = " $_POST[$column] . " ";            
    }
}

$query .= " WHERE bundel_id = " . $_POST['bundel_id'];
like image 44
user2883814 Avatar answered Sep 30 '22 21:09

user2883814