简体   繁体   中英

php sql update only filled in fields

(this is all basic php voor school) I made a form where you can update your account information, when you hit the submit button, it will come to this php code. If a field is not filled in, it does not need to be updated, I tried "WHERE field IS NOT NULL" but it does not seem to work, it gives an empty record...

(variables are all in dutch, sorry)

$klantnummer = $_COOKIE['klantnummer'];
$naam =($_POST["naam"]);
$adres =($_POST["adres"]);
$postcode =($_POST["postcode"]);
$gemeente =($_POST["gemeente"]);
$leden =($_POST["gezinsleden"]);
$huidigemeterstand =($_POST["huidigemeterstand"]);
$vorigemeterstand =($_POST["vorigemeterstand"]);
$provincie =($_POST["provincie"]);


//set up connection and choose database
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("opdracht3", $con);
mysql_query("UPDATE waterstand SET naam = '$naam', adres = '$adres', postnummer = '$postcode', gemeente = '$gemeente', vorigemeterstand='$vorigemeterstand', huidigemeterstand='$huidigemeterstand', provincie='$provincie', aantalgezinsleden = '$gezinsleden'
WHERE klantnummer = '$klantnummer' AND naam IS NOT NULL");`

ofcourse I need to ad the rest of the 'field' IS NOT NULL but for example I only use 'naam'.. but it does not work :/

It's probably easiest if you keep this part of the logic away from the database, eg

<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

if ( !mysql_select_db("opdracht3", $con) ) {
    die('Could not connect: ' . mysql_error());
}

// contains strings/parameters/identifiers ready-for-use with mysql  
$sql_params = array(
    'fields' => array('naam', 'adres', 'postcode', 'gemeente', 'gezinsleden', 'huidigemeterstand', 'vorigemeterstand', 'provincie'),
    'klantnummer' => mysql_real_escape_string($_COOKIE['klantnummer']),
    'updates' => array()
);

// the names of the POST-fields are the same as the column identifiers of the database table
// go through each identifier
foreach( $sql_params['fields'] as $f ) {
    // put in here the conditions for "an empty field, e.g.
    // if there is such POST-field and its value is one or more "usable" characters long
    if ( isset($_POST[$f]) && strlen(trim($_POST[$f])) > 0 ) {
        // create a new `x`=`y` "line" as in UPDATE ... SET `x`='y'
        // and append it to the array $sql_params['updates']
        $sql_params['updates'][] = sprintf("`%s`='%s'", $f, mysql_real_escape_string($_POST[$f]));
    }
}

// the "lines" in $sql_params['updates'] need to be concatenated with , between them
// join(', ', $sql_params['updates']) does that
// insert that string and the parameter klantnummer into the query string
$query = sprintf(
    "
        UPDATE
            waterstand
        SET
            %s
        WHERE
            klantnummer = '%s'
    ",
    join(', ', $sql_params['updates']),
    $sql_params['klantnummer']
);

(untested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM