简体   繁体   English

php sql update仅填写字段

[英]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. (这是所有基本的php voor学校)我制作了一个表格,您可以在其中更新您的帐户信息,当您单击“提交”按钮时,它将显示此php代码。 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... 如果未填写字段,则不需要更新,我尝试了“ WHERE field IS NOT NULL”,但它似乎不起作用,它提供了一个空记录...

(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 :/ 当然,我需要声明“字段”的其余部分不是NULL,但是例如,我仅使用“ naam” ..但它不起作用:/

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) (未试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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