2

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\r\n\Z\n\0\0\0\rIHDR\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?

10
  • Echo $img, is it the uploaded image? Commented Aug 4, 2018 at 0:36
  • I won't post the whole BLOB here. But when I echo $img; it returns a BLOB starting with what I understand it should: ‰PNG Commented Aug 4, 2018 at 0:46
  • php.net/manual/en/pdo.lobs.php Commented Aug 4, 2018 at 0:47
  • 1
    You are never actually executing the $sql Commented Aug 4, 2018 at 0:49
  • 1
    your UPDATE won't run properly, look at the syntax dev.mysql.com/doc/refman/8.0/en/update.html Commented Aug 4, 2018 at 1:00

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.