Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php sql更新仅在字段中填写_Php_Sql - Fatal编程技术网

php sql更新仅在字段中填写

php sql更新仅在字段中填写,php,sql,Php,Sql,(这是所有的基础课程或学校) 我制作了一个表单,您可以在其中更新您的帐户信息,当您点击提交按钮时,它将出现在这个php代码中。如果一个字段没有填写,它就不需要更新,我尝试了“wherefieldnotnull”,但它似乎不起作用,它给出了一个空记录 (对不起,变量都是荷兰语) 当然,我需要说明“字段”的其余部分不是空的,但例如,我只使用“naam”。。但是它不起作用:/如果您将这部分逻辑远离数据库,可能是最简单的 <?php $con = mysql_connect("localhost"

(这是所有的基础课程或学校) 我制作了一个表单,您可以在其中更新您的帐户信息,当您点击提交按钮时,它将出现在这个php代码中。如果一个字段没有填写,它就不需要更新,我尝试了“wherefieldnotnull”,但它似乎不起作用,它给出了一个空记录

(对不起,变量都是荷兰语)


当然,我需要说明“字段”的其余部分不是空的,但例如,我只使用“naam”。。但是它不起作用:/

如果您将这部分逻辑远离数据库,可能是最简单的

<?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']
);

首先停止使用
mysql_uu>函数
,它们已被弃用。开始使用PDO,如果数据库字段中的默认值设置为NULL会更好、更安全吗?如果设置为“”或类似值,则IS NULL check将不会返回true。@本·凯里:这是我们在学校学习的方式,但我会检查出来,谢谢@Maccath:Thx,我会看看这是否是问题所在。WHERE子句“引用”了更新之前的记录,也就是说,在可能的更新发生之前,您的非空条件测试记录是否有空值。哇,非常感谢@VolkerK,但我认为代码对我来说不可读:D,我们正在学习基础知识,我不理解这段代码,很抱歉:/。我查看了您提供给我的筛选器页面,我想使用filter\u has\u var函数,对吗?很抱歉,该链接可能会误导您,因为它在修改查询字符串方面帮不了您多少忙。添加到示例脚本的注释。
<?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']
);