用PHP和MySQL更新记录中的BLOB

时间:2018-08-04 00:28:04

标签: php mysql database

很明显,我有非常有限的PHP / MySQL知识。

我有一个表单,可以在其中使用teamID(一个int主键)调用现有记录。每个记录都有三列。我需要能够更新teamLogo实体中每个记录的第三列,称为Team。这是现有的BLOB。

这是我的PHP:

function updateTeamLogo() {
  global $server, $db, $dbUser, $dbKey;

  try {
    $conn = new PDO("mysql:host=" . $server . ";dbname=" . $db, $dbUser, $dbKey);
    $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $file = $_FILES["teamLogo"]["tmp_name"];

    if(!isset($file)) {
      echo "Please select an image to upload";
    } else {
      $fileSize = getimagesize($_FILES["teamLogo"]["tmp_name"]);

      if ($fileSize) {
        $img = file_get_contents($_FILES["teamLogo"]["tmp_name"]);
        $sql = $conn -> prepare("UPDATE Team SET (teamID, teamLogo) VALUES (:teamID, :teamLogo) WHERE teamID=:teamID");
        $sql -> bindValue(":teamID", $_POST["teamID"]);
        $sql -> bindValue(":teamLogo", $img);

        $result = $sql -> execute();

        if ($result == null) {
          echo "Error uploading image";
        } else {
          echo "Image uploaded";
        }
      } else {
        echo "The file to be uploaded is not an image";
      }
    }
  }

  catch(PDOException $e) {
    echo "An error occured: " . $e -> getMessage();
  }

  $conn = null;
}

if (isset($_POST["updateTeam"])) {
  updateTeamLogo();
}

这是我的标记:

<form method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <label>Team ID*</label>
  <input class="small" type="text" name="teamID" placeholder="9" value="<?php echo $teamID; ?>">
  <label>Team name</label>
  <input class="small" type="text" name="teamName" placeholder="Watson's Bay Warriors" value="<?php echo $teamName; ?>">
  <label>Team logo</label>
  <input type="file" name="teamLogo">
  <input type="submit" name="getTeam" value="View">
  <input type="submit" name="updateTeam" value="Update">
</form>

当我对此进行测试时,PHP会回显:

  

发生错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法中有错误;请参见表11。检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'(teamID,teamLogo)VALUES('1','PNG \ r \ n \ Z \ n \ 0 \ 0 \ 0 \ rIHDR \ 0 \ 0 \0�\ 0 \ 0 \0�\ 0 \'在第1行

我的sql语句中出现什么错误,导致BLOB无法正确更新?

1 个答案:

答案 0 :(得分:4)

您的UPDATE查询无效。

将代码更改为以下内容:

$sql = $conn -> prepare("UPDATE Team SET teamID=:teamID, teamLogo=:teamLogo 
                         WHERE teamID=:teamID");

手册显示了正确的语法:

即:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]