我,我不知道我做错了什么,但我试图用ajax与ajax一起使用它并不能正常工作......我想。 ajax返回成功,但它不会像我需要的那样插入和更新到数据库中..这里是代码
AJAX
$('.fa-thumbs-o-up').click(function()
{
$.ajax({
url:"insert.php",
type:"post",
success:function()
{
// it returns alert("HEY"); but doesn't insert :X
alert("Hey");
}
});
});
PHP
require 'AnswerVoteManager.php';
require '../config.php';
if(isset($_POST['upvote']))
{
$title = $_GET['title'];
$answervotemanager = new AnswerVoteManager($dbh);
$answervotemanager->setVoteType('Upvote');
$answervotemanager->setAnswerID($_POST['answerID']);
$answervotemanager->setVoter($_SESSION['username']);
$answervotemanager->setTitle($title);
$answervotemanager->vote();
}
php oop class
private function addVote()
{
$sql = "UPDATE question_answers set count = question_answers.count + 1 WHERE id=:id";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':id',$this->answerID);
$stmt->execute();
}
private function subtractVote()
{
$sql = "UPDATE question_answers set count = question_answers.count - 1 WHERE id=:id";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':id',$this->answerID);
$stmt->execute();
}
private function deleteVote()
{
$sql = "DELETE FROM answer_votes where answerID=:answerid and username=:username LIMIT 1";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':answerid',$this->getAnswerID());
$stmt->bindValue(':username',$this->voter);
$stmt->execute();
$this->subtractVote();
$this->redirect();
}
private function insertVote()
{
$sql = "INSERT into answer_votes (vote_type,answerID,username,questiontitle) values (:vote_type,:answerid,:username,:questiontitle)";
$stmt = $this->dbh->prepare($sql);
$stmt->bindValue(':vote_type',$this->getVoteType());
$stmt->bindValue(':answerid',$this->getAnswerID(),PDO::PARAM_INT);
$stmt->bindValue(':username',$this->getVoter());
$stmt->bindValue(':questiontitle',$this->getTitle());
$stmt->execute();
$this->addVote();
$this->redirect();
}
public function vote()
{
if($this->vote_type == 'Upvote' && $this->voted()) {
$this->deleteVote();
} else {
$this->insertVote();
}
}