我如何防止MySQL更新列与一个空字符串使用PDO


How do I prevent MySQL from updating column with an empty string using PDO

我有一个确切的问题,我如何防止MySQL更新列与一个空字符串

然而,唯一的变化是我将此应用于PDO而不是传统的MySQL。

我遵循了关于添加IF语句以检查长度是否为0的问题的顶部答案。这似乎并不适用于PDO,然而,因为没有更新发生在表和网页输出:Array ( [0] => HY093 [1] => [2] => ) 1。注意我是通过bind函数使用预处理语句。

包含IF语句的PHP代码:

    <?php
$pdo=new PDO("mysql:dbname=createyo_TestDatabase;host=localhost","createyo_james","password");
$statement=$pdo->prepare(
"UPDATE `Users` 
SET `EditIDs` = IF(LENGTH(':EditID')=0, EditIDs, ':EditID'), 
`ArticleIDs` = IF(LENGTH(':ArticleID')=0, ArticleIDs, ':ArticleID'),
`Reputation` = IF(LENGTH(':Reputation')=0, Reputation, ':Reputation'),
`VotedUpArticleIDs` = IF(LENGTH(':VotedUpArticleID')=0, VotedUpArticleIDs, ':VotedUpArticleID'),
`VotedUpEditIDs` = IF(LENGTH(':VotedUpEditID')=0, VotedUpEditIDs, ':VotedUpEditID'),
`VotedDownArticleIDs` = IF(LENGTH(':VotedDownArticleID')=0, VotedDownArticleIDs, ':VotedDownArticleID'),
`VotedDownEditIDs` = IF(LENGTH(':VotedDownEditID')=0, VotedDownEditIDs, ':VotedDownEditID'),
WHERE `UserID` = :UserID");
$statement->bindValue(':EditID', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
$statement->bindValue(':Reputation', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);
    $statement->bindValue(':UserID', (string) trim($_GET['UserID']), PDO::PARAM_STR);
    $statement->execute() or die(print_r($statement->errorInfo()));
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
print $json;
?>

编辑

我现在收到一个语法错误:你有一个错误在你的SQL语法;检查与您的MySQL服务器版本对应的手册,以便在第9行'WHERE UserID = '1234 "附近使用正确的语法。我一直没能理解这个错误。

新的PHP代码:

$statement=$pdo->prepare(
"UPDATE `Users` 
SET `EditIDs` = IF(LENGTH(:EditID)=0, EditIDs, :EditID1), 
`ArticleIDs` = IF(LENGTH(:ArticleID)=0, ArticleIDs, :ArticleID1),
`Reputation` = IF(LENGTH(:Reputation)=0, Reputation, :Reputation1),
`VotedUpArticleIDs` = IF(LENGTH(:VotedUpArticleID)=0, VotedUpArticleIDs, :VotedUpArticleID1),
`VotedUpEditIDs` = IF(LENGTH(:VotedUpEditID)=0, VotedUpEditIDs, :VotedUpEditID1),
`VotedDownArticleIDs` = IF(LENGTH(:VotedDownArticleID)=0, VotedDownArticleIDs, :VotedDownArticleID1),
`VotedDownEditIDs` = IF(LENGTH(:VotedDownEditID)=0, VotedDownEditIDs, :VotedDownEditID1),
WHERE `UserID` = :UserID");
$statement->bindValue(':EditID', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
    $statement->bindValue(':Reputation', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);
$statement->bindValue(':EditID1', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID1', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
$statement->bindValue(':Reputation1', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID1', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID1', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID1', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID1', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);

在SQL中,您不应该将:placeholder放在引号内。这使得它们变成字面字符串,因此它们不会从绑定的形参中被替换。

$statement=$pdo->prepare(
"UPDATE `Users` 
SET `EditIDs` = IF(LENGTH(:EditID)=0, EditIDs, :EditID), 
`ArticleIDs` = IF(LENGTH(:ArticleID)=0, ArticleIDs, :ArticleID),
`Reputation` = IF(LENGTH(:Reputation)=0, Reputation, :Reputation),
`VotedUpArticleIDs` = IF(LENGTH(:VotedUpArticleID)=0, VotedUpArticleIDs, :VotedUpArticleID),
`VotedUpEditIDs` = IF(LENGTH(:VotedUpEditID)=0, VotedUpEditIDs, :VotedUpEditID),
`VotedDownArticleIDs` = IF(LENGTH(:VotedDownArticleID)=0, VotedDownArticleIDs, :VotedDownArticleID),
`VotedDownEditIDs` = IF(LENGTH(:VotedDownEditID)=0, VotedDownEditIDs, :VotedDownEditID),
WHERE `UserID` = :UserID");

但是,您也不允许在同一查询中多次使用相同的:placeholder。因此,您需要为重复的占位符提供不同的名称。

$statement=$pdo->prepare(
"UPDATE `Users` 
SET `EditIDs` = IF(LENGTH(:EditID)=0, EditIDs, :EditID1), 
`ArticleIDs` = IF(LENGTH(:ArticleID)=0, ArticleIDs, :ArticleID1),
`Reputation` = IF(LENGTH(:Reputation)=0, Reputation, :Reputation1),
`VotedUpArticleIDs` = IF(LENGTH(:VotedUpArticleID)=0, VotedUpArticleIDs, :VotedUpArticleID1),
`VotedUpEditIDs` = IF(LENGTH(:VotedUpEditID)=0, VotedUpEditIDs, :VotedUpEditID1),
`VotedDownArticleIDs` = IF(LENGTH(:VotedDownArticleID)=0, VotedDownArticleIDs, :VotedDownArticleID1),
`VotedDownEditIDs` = IF(LENGTH(:VotedDownEditID)=0, VotedDownEditIDs, :VotedDownEditID1)
WHERE `UserID` = :UserID");

那么你必须绑定所有额外的参数:

$statement->bindValue(':EditID1', (string) trim($_GET['EditID']), PDO::PARAM_STR);
$statement->bindValue(':ArticleID1', (string) trim($_GET['ArticleID']), PDO::PARAM_STR);
$statement->bindValue(':Reputation', (string) trim($_GET['Reputation']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpArticleID1', (string) trim($_GET['VotedUpArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedUpEditID1', (string) trim($_GET['VotedUpEditID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownArticleID1', (string) trim($_GET['VotedDownArticleID']), PDO::PARAM_STR);
$statement->bindValue(':VotedDownEditID1', (string) trim($_GET['VotedDownEditID']), PDO::PARAM_STR);

拥有自己的trim函数,如

  function nulltrim($str)
  {
      $s = trim($str);
      if ( empty($s) ) 
            return null;
      return $s;
  }

  $statement->bindValue(':EditID', (string) nulltrim($_GET['EditID']), PDO::PARAM_STR);