如何使用PDO阻止MySQL使用空字符串更新列

时间:2014-08-22 23:15:00

标签: php mysql sql pdo prepared-statement

我的确切问题为How do I prevent MySQL from updating column with an empty string

然而,唯一的转折是我将它应用于PDO而不是传统的MySQL。

我已经按照关于添加IF语句的问题的最佳答案来检查长度是否为0.这似乎不适用于PDO,但是因为表格和网页输出中没有更新:Array ( [0] => HY093 [1] => [2] => ) 1 。注意我通过bind函数使用预准备语句。

我的PHP代码包含IF语句:

    <?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服务器版本相对应的手册,以便使用“UserID =&#39; 1234&#39;&#39;&#39;&#39;&#39;&#39;&#39;在第9行。我无法理解这个错误。

新的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);

2 个答案:

答案 0 :(得分:3)

在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);

答案 1 :(得分:0)

拥有自己的trim功能

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

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

应该解决它。