【问题标题】:sql - updating without overriding existing informationsql - 在不覆盖现有信息的情况下进行更新
【发布时间】:2010-10-19 18:33:19
【问题描述】:

我有以下条件代码,如果具有适当用户名的记录不存在,它将插入一条新记录,如果存在则更新记录。这很好用。

但是,目前,如果我插入一条新记录,并且只插入名字和姓氏,也许还有地址详细信息,比如电话信息留空,这很好。如果我希望仅使用电话记录更新记录,则姓名和地址信息将被替换为任何内容。

我想知道的是,是否可能有一种简单的方法来填充我的 php html 表单以更新信息,以及我将要更新的字段的内容?我正在使用

 <input type="text" name="uniquename" />

获取用户输入,然后将其传递给一个 javascript 函数,该函数随后将传递回下面的 php 代码。如果这是不可能的,有没有一种简单的方法来使用一些 sql/php 魔法,只更新与用户输入相对应的字段,该字段不为空?

$usernameQuery = "select username from USERS where username = '" . $con->escape_string($username) . "'"; 

$xblah = $con->query($usernameQuery);
  while ($row = mysqli_fetch_assoc($xblah))
  {
    $checkUsername = $row['username'];
   }

if ($checkUsername == null) {

$userQuery = "INSERT INTO USERS VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if ($userInfo = $con->prepare($userQuery)) {
    $userInfo->bind_param("ssssssssssssssssssss", $username, $firstname, $lastname, $flaggedauctions, $lastauction, $street1, $city1, $postcode1, $street2, $city2, $postcode2, $phone, $mobilephone, $fax, $email, $website, $bank, $banknumber, $accountnumber, $comments);
    $userInfo->execute();
    $userInfo->close();
    echo "true";
} else {
echo "false";
}
print_r($con->error);
}

else if ($checkUsername == $username) {

$userQuery = "UPDATE USERS SET firstname = ?, lastname = ?, flaggedauctions = ?, lastauction = ?, street1 = ?, city1 = ?, postcode1 = ?, street2 = ?, city2 = ?, postcode2 = ?, phone = ?, mobilephone = ?, fax = ?, email = ?, website = ?, bank = ?, banknumber = ?, accoutnumber = ? WHERE username = ?";
if ($userInfo = $con->prepare($userQuery)) {
    $userInfo->bind_param("sssssssssssssssssss", $firstname, $lastname, $flaggedauctions, $lastauction, $street1, $city1, $postcode1, $street2, $city2, $postcode2, $phone, $mobilephone, $fax, $email, $website, $bank, $banknumber, $accountnumber, $username);
    $userInfo->execute();
    $userInfo->close();
    echo "true";
} else {
echo "false";
}
print_r($con->error);
}

【问题讨论】:

    标签: php javascript sql mysql


    【解决方案1】:

    您只需要动态构建更新语句和绑定参数。 例如

    $userQuery = "UPDATE ";
    
    if (!empty($firstname)) {
      $userQuery += "firstname = ?,";
    }
    
    ...    
    
    if (!empty($firstname)) {
      $bindArgs[] = $firstname;
    }
    ...
    

    因此您的查询不会更新不应更改的列。

    【讨论】:

    • 我的 PHP 技能有点生疏,但我认为您可以将 $bindArgs 传递给 bind_param 函数。我相信您可以将数组传递给 PHP 中的可变数字参数函数。
    • 所以它们是两个单独的解决方案?我只想为每个变量设置 bindARgs?
    • 不不,它们是相同的解决方案。因为您有条件地构建查询,所以您也必须动态构建绑定变量列表。
    • 你要做的是建立你的 $bindArgs 数组,然后通过这种方式将它传递给 bind_param 函数: $userInfo->bind_param("ssssssssssssssssss", $bindArgs) 就像我说的我的 PHP 技能是有点生疏,但我认为这很有效,因为可变长度参数列表函数也应该接受数组。
    • 顺便说一句,我的意思是你这样构建你的 $bindArgs 数组: if (!empty($firstname)) $bindArgs[] = $firstname; if (!empty($lastname)) $bindArgs[] = $lastname; if (!empty($flaggedauctions)) $bindArgs[] = $flaggedauctions; ...
    【解决方案2】:

    更一般地,请注意:

    • 明智的做法是在 INSERT ... VALUES 中指定列名,否则如果有人添加列,您会很生气,如果有人对列重新排序,您会感到困惑。

    • 在您的选择和更新之间存在竞争条件......另一个进程可能会在您仍在思考时插入/删除一行,从而导致您的插入/更新失败考虑:

      • 插入 ... 在重复密钥更新时 ...
      • 替换成...

    【讨论】:

    • 如何插入重复或替换修复竞争条件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    相关资源
    最近更新 更多