$conn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
不会工作,因为XAMPP没有MySQL 5.6,我无法弄清楚如何使新版本正常工作。有没有办法在5.6之前进行这种读/写事务?将其留空或放入MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT
似乎不会使任何插入/更新工作。
PHP函数有任何帮助:
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();
}