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

使用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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化