I am trying to use an INSERT ON DUPLICATE KEY UPDATE
query in PHP
and MySQL
.
I have a user profile where a user can update an image to their profile (where it then gets saved into the DB). My issue is, if inside of the profileImage
table they already have an entry in there (and the user can be identified with studentID
, then only run an UPDATE
on the image with just changing the filePath (which holds the name of the image) and do not insert another row into the db table.
However I'm having issues with the query still allowing users to upload more than 1 image.
php script:
$stmt = $conn->prepare ("INSERT INTO `profileImage` (`imageID`, `imagePath`, `studentID`) VALUES (NULL, ?, ?) ON DUPLICATE KEY UPDATE `imageID` = VALUES (`imageID`), `imagePath` = VALUES (`imagePath`), `studentID` = VALUES (`studentID`) ");
$stmt->bind_param("si", $fileName, $studentID);
$stmt->execute() or die("Failed to insert image into the database");
profileImage table in the db:
imageID
is primary key and studentID
is a foreign key, is there anyway I can set the studentID
in my statement as the trigger, so if the studentID
already exists in this table, THEN run the update rather than the insert?