仅更新不为空的字段 [英] Only update fields that are not null

查看:84
本文介绍了仅更新不为空的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找出仅当字段不为null时才更新字段的方法(否则NULL会覆盖未更改的字段)。

I have been trying to figure out how to update the fields only if they are not null (otherwise NULL overrides the unchanged fields). It doesn't matter that the fields can not be null.

我已经尝试了COALESCE和ISNULL,但是我一定不能以正确的方式使用它们'不习惯Dreamweaver编码。代码如下:

I have tried both the COALESCE and ISNULL, however I must not be using them in the right way as I'm not use to the Dreamweaver coding. The code is below:

<i>if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "catalogue")) {
  $updateSQL = sprintf("UPDATE catalogue SET catbox1=%s, catbox2=%s, catbox3=%s, catbox4=%s, catbox5=%s, catbox6=%s WHERE id=%s",
                       GetSQLValueString($_POST['catbox1'], "text"),
                       GetSQLValueString($_POST['catbox2'], "text"),
                       GetSQLValueString($_POST['catbox3'], "text"),
                       GetSQLValueString($_POST['catbox4'], "text"),
                       GetSQLValueString($_POST['catbox5'], "text"),
                       GetSQLValueString($_POST['catbox6'], "text"),
                       GetSQLValueString($_POST['id'], "int"));</i>


推荐答案

您的代码中几乎没有错误,但是这些可以

There are few mistakes in you code, But these can be typos.


  1. 没有PHP包装:您的mysqlcode中包含了部分内容

  2. 使用 IS NOT NULL 而不是 ISNULL

  1. NO PHP Wrapper: You have include your mysqlcode with portion
  2. Use IS NOT NULL instead of ISNULL

您尝试做的基本方式是使用这样的查询

Basic way of what you are trying to do is use a query like this

UPDATE `tablename` SET `fieldName` = 'value' WHERE `fieldName` IS NOT NULL

这篇关于仅更新不为空的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆