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?