douhu3424
2018-08-04 00:28
浏览 148
已采纳

使用PHP和MySQL更新记录中的BLOB

It's probably obvious I have very limited PHP / MySQL knowledge.

I have a form where I can call existing records using a teamID (an int primary key). Each record has three columns. I need the ability to update each record's third column called teamLogo in my Team entity. It is an existing BLOB.

Here is my 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();
}

And here is my markup:

<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>

When I test this, the PHP echoes:

An error occured: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(teamID, teamLogo) VALUES ('1', '�PNG \Z \0\0\0IHDR\0\0\0�\0\0\0�\0\' at line 1

What is the error in my sql statement that stops the BLOB from being correctly updated?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulianqi3870 2018-08-04 01:05
    已采纳

    Your UPDATE query is invalid.

    Change your code to the following:

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

    The manual shows the proper syntax:

    I.e.:

    UPDATE [LOW_PRIORITY] [IGNORE] table_reference
        SET assignment_list
        [WHERE where_condition]
        [ORDER BY ...]
        [LIMIT row_count]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报