I'm trying to update my datebase depending on whether a row already exists or not.
In my php file I create this string:
IF EXISTS(SELECT updown
FROM voted_source
WHERE ID = ? AND userID = ?)
BEGIN
UPDATE voted_source
SET updown = TRUE
WHERE ID = ?;
UPDATE source
SET score_up = score_up + 1,
score_down = score_down - 1
WHERE ID = ?;
END;
ELSE
BEGIN
INSERT INTO voted_source (ID, userID, updown)
VALUES (?, ?, TRUE);
UPDATE source
SET score_up = score_up + 1,
score_down = score_down - 1
WHERE ID = ?;
END
And I call this using pdo like so:
$pdo = new PDO('mysql: host='.$servername.';dbname='.$db, $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
$stmt = $pdo->prepare($sqlString);
$stmt->execute([$sourceID, $userID, $sourceID, $sourceID, $sourceID, $userID, $sourceID ]);
$pdo->commit();
$pdo = null;
This returns the error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BEGIN UPDATE voted_source SET updown = TRUE WHERE ID = '1'at line 4' in voteSource.php:74 Stack trace: #0 voteSource.php(74): PDOStatement->execute(Array) #1 {main} thrown in voteSource.php on line 74;
The user has the rights to SELECT INSERT and UPDATE. Any help is greatly aprreciated.