用php和mysql更新record中的blob

o8x7eapl  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(289)

很明显,我对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语法有错误;请查看与您的mysql服务器版本对应的手册,以了解使用near'(teamid,teamlogo)值('1','2')的正确语法�png\r\n\z\n\0\0\0\rihdr\0\0\0�\0\0\0�\第1行的“0”
我的错误是什么 sql 语句来阻止blob正确更新?

xfb7svmp

xfb7svmp1#

您的更新查询无效。
将代码更改为以下内容:

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

手册显示了正确的语法:
https://dev.mysql.com/doc/refman/8.0/en/update.html
即。:

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

相关问题