$conn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); wont work because XAMPP doesn't have MySQL 5.6, and I can't figure out for the life of me how to get a new version working. Is there a way to have this read/write transaction pre-5.6? Leaving it blank or putting in MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT seems to not make any inserts/updates work.
PHP function it's any help:
function vote($accid, $postid, $votedir) {
include $_SERVER['DOCUMENT_ROOT'].'/database.php';
$conn = new mysqli($sqlservername, $sqlusername, $sqlpassword, $sqldbname);
$conn->begin_transaction();
$stmt = $conn->prepare('SELECT vote FROM votes WHERE accid = ? AND postid = ?');
$stmt->bind_param('ss', $accid, $postid);
$stmt->execute();
$result = $stmt->get_result();
$updateStmt = null;
if ($result->num_rows == 0) {
$updateStmt = $conn->prepare('INSERT INTO votes (accid, postid, vote) VALUES (?, ?, ?)');
$updateStmt->bind_param('ssi', $accid, $postid, $votedir);
} else {
$existingVote = $result->fetch_assoc()['vote'];
$updateStmt = $conn->prepare('UPDATE votes SET vote = ? WHERE accid = ? AND postid = ?');
if ($existingVote == $votedir) {
// They're undoing their existing vote
$updateStmt->bind_param('iss', 0, $accid, $postid);
} else {
// They haven't voted before
$updateStmt->bind_param('iss', $votedir, $accid, $postid);
}
}
$conn->commit();
}