我正在为不同的按钮工作3个代码,我做了但我不知道为什么按钮不起作用,并没有给我显示错误! 有人可以帮助我(顺便说一句,我使用的是xammp for sql)代码如下:
like.js
$(function(){
$(document).on('click', '.like-btn', function(){
var tweet_id = $(this).data('tweet');
var user_id = $(this).data('user');
var counter = $(this).find('.likesCounter');
var count = counter.text()
var button = $(this);
$.post('http://localhost/twitter/core/ajax/like.php', {like:tweet_id, user_id:user_id,}, function(){
counter.show();
button.addClass('unlike-btn');
button.removeClass('like-btn');
count++;
counter.text(count);
button.find('.fa-heart-o').addClass('fa-heart');
button.find('.fa-heart').removeClass('fa-heart-o');
});
});
$(document).on('click', '.unlike-btn', function(){
var tweet_id = $(this).data('tweet');
var user_id = $(this).data('user');
var counter = $(this).find('.likesCounter');
var count = counter.text()
var button = $(this);
$.post('http://localhost/twitter/core/ajax/like.php', {unlike:tweet_id, user_id:user_id,}, function(){
counter.show();
button.addClass('like-btn');
button.removeClass('unlike-btn');
count--;
if (count === 0) {
counter.hide();
}else{
counter.text(count);
}
button.find('.fa-heart').addClass('fa-heart-o');
button.find('.fa-heart-o').removeClass('fa-heart');
});
});
});
like.php
<?php
include '../init.php';
if(isset($_POST['like']) && !empty($_POST['like'])){
$user_id = $_SESSION['user_id'];
$tweet_id = $_POST['like'];
$get_id = $_POST['user_id'];
$getFromT->addLike($user_id, $tweet_id, $get_id);
}
if(isset($_POST['unlike']) && !empty($_POST['unlike'])){
$user_id = $_SESSION['user_id'];
$tweet_id = $_POST['unlike'];
$get_id = $_POST['user_id'];
$getFromT->unlike($user_id, $tweet_id, $get_id);
}
?>
tweet.php
<?php
class Tweet extends User {
function __construct($pdo){
$this->pdo = $pdo;
}
public function tweets($user_id){
$stmt = $this->pdo->prepare("SELECT * FROM `tweets`,`users` WHERE `tweetBy` = `user_id`");
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($tweets as $tweet) {
$likes = $this->likes($user_id, $tweet->tweetID);
echo '<div class="all-tweet">
<div class="t-show-wrap">
<div class="t-show-inner">
<!-- this div is for retweet icon
<div class="t-show-banner">
<div class="t-show-banner-inner">
<span><i class="fa fa-retweet" aria-hidden="true"></i></span><span>Screen-Name Retweeted</span>
</div>
</div>
-->
<div class="t-show-popup">
<div class="t-show-head">
<div class="t-show-img">
<img src="'.$tweet->profileImage.'"/>
</div>
<div class="t-s-head-content">
<div class="t-h-c-name">
<span><a href="'.$tweet->username.'">'.$tweet->screenName.'</a></span>
<span>@'.$tweet->username.'</span>
<span>'.$tweet->postedOn.'</span>
</div>
<div class="t-h-c-dis">
'.$this->getTweetLinks($tweet->status).'
</div>
</div>
</div>';
if(!empty($tweet->tweetImage)){
echo'<!--tweet show head end-->
<div class="t-show-body">
<div class="t-s-b-inner">
<div class="t-s-b-inner-in">
<img src="'.$tweet->tweetImage.'" class="imagePopup"/>
</div>
</div>
</div>
<!--tweet show body end-->';
}
echo'</div>
<div class="t-show-footer">
<div class="t-s-f-right">
<ul>
<li><button><a href="#"><i class="fa fa-share" aria-hidden="true"></i></a></button></li>
<li><button><a href="#"><i class="fa fa-retweet" aria-hidden="true"></i></a></button></li>
<li>'.(($likes['likeOn'] === $tweet->tweetID) ? '<button class="unlike-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-heart-o" aria-hidden="true"></i><span class="likesCounter">'.$tweet->likesCount.'</span></button>' : '<button class="like-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-heart" aria-hidden="true"></i><span class="likesCounter">'.(($tweet->likesCount > 0) ? $tweet->likesCount : '').'</span></button>').'</li>
<li>
<a href="#" class="more"><i class="fa fa-ellipsis-h" aria-hidden="true"></i></a>
<ul>
<li><label class="deleteTweet">Delete Tweet</label></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>';
}
}
public function getTrendByHash($hashtag){
$stmt = $this->pdo->prepare("SELECT * FROM `trends` WHERE `hashtag` LIKE :hashtag LIMIT 5");
$stmt->bindValue(':hashtag', $hashtag.'%');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
public function getMention($mention){
$stmt = $this->pdo->prepare("SELECT `user_id`,`username`,`screenName`,`profileImage` FROM `users` WHERE `username` LIKE :mention OR `screenName` LIKE :mention LIMIT 5");
$stmt->bindValue(':mention', $mention. '%');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
public function addTrend($hashtag){
preg_match_all("/#+([a-zA-Z0-9_]+)/i", $hashtag, $matches);
if ($matches) {
$result = array_values($matches[1]);
}
$sql = "INSERT INTO `trends` (`hashtag`,`createdOn`) VALUES(:hashtag, CURRENT_TIMESTAMP)";
foreach ($result as $trend) {
if ($stmt = $this->pdo->prepare($sql)){
$stmt->execute(array(':hashtag' => $trend));
}
}
}
public function getTweetLinks($tweet){
$tweet = preg_replace("/(https?:\/\/)([\w]+.)([\w\.]+)/", "<a href='$0' target='_blink'>$0</a>", $tweet);
$tweet = preg_replace("/#([\w]+)/", "<a href='".BASE_URL."hashtag/$1'>$0</a>", $tweet);
$tweet = preg_replace("/@([\w]+)/", "<a href='".BASE_URL."$1'>$0</a>", $tweet);
return $tweet;
}
public function addLike($user_id, $tweet_id, $get_id){
$stmt = $this->pdo->prepare("UPDATE `tweets` SET `likesCount` = `likesCount` +1 WHERE `tweetID` = :tweet_id");
$stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->execute();
$this->create('likes', array('likeBy' => $user_id, 'likeOn' => $tweet_id));
}
public function unlike($user_id, $tweet_id, $get_id){
$stmt = $this->pdo->prepare("UPDATE `tweets` SET `likesCount` = `likesCount` -1 WHERE `tweetID` = :tweet_id");
$stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->execute();
$stmt = $this->pdo->prepare("DELETE FROM `likes` WHERE `likeBy` = :user_id AND `likeOn` = :tweet_id");
$stmt = bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt = bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->execute();
}
public function likes($user_id, $tweet_id){
$stmt = $this->pdo->prepare("SELECT * FROM `likes` WHERE `likeBy` = :user_id AND `likeOn` = :tweet_id");
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
}
?>
所以任何人都知道如何解决问题!!
答案 0 :(得分:0)
问题在于bindParam
查询中的两次DELETE
次调用。您的$stmt = bindParam(
应为$stmt->bindParam(
:
public function unlike($user_id, $tweet_id, $get_id){
$stmt = $this->pdo->prepare("UPDATE `tweets` SET `likesCount` = `likesCount` -1 WHERE `tweetID` = :tweet_id");
$stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->execute();
$stmt = $this->pdo->prepare("DELETE FROM `likes` WHERE `likeBy` = :user_id AND `likeOn` = :tweet_id");
// $stmt = bindParam(":user_id", $user_id, PDO::PARAM_INT);
// $stmt = bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
$stmt->execute();
}