在数据库中存储相似和不同数据

时间:2018-12-06 13:57:57

标签: javascript php jquery html ajax

我正在开发“喜欢和不同”功能,以便用户可以对产品喜欢/不喜欢。

按照本教程https://www.sourcecodester.com/tutorials/php/11588/easy-and-simple-likeunlike-using-ajaxjquery.html

的介绍,我已完成所有工作

问题在于它没有将任何内容存储到数据库中(例如表)。 没有显示错误。当我单击“赞”时,它显示“应有的区别”,仅此而已-赞计数器不会增加,并且数据库中没有任何存储。

index.php

<?php
$bid = $_GET['id'];
$stmt = "SELECT * FROM `like` WHERE postid = :postid AND userid = :userid";
if($querys = $pdo->prepare($stmt)){
    $querys->bindValue(':postid', $bid);
    $querys->bindValue(':userid', $userid);
    $querys->execute();
    $check = $querys->rowCount();
}

if($check>0)
{
    $like = '<button value="'.$bid.'" class="unlike">Unlike</button>';
}else{
    $like = '<button value="'.$bid.'" class="like">Like</button>';
}

$sql = "SELECT count(*) FROM `like` WHERE postid = :postid";

if($query = $pdo->prepare($sql)){
    $query->bindValue(':postid', $bid);
    $query->execute();
    $nlikes = $query->fetchColumn();
    //rest of code here
}
echo $like;
<span id="show_like<?php echo $bid; ?>">
<?php echo $nlikes;
?>

javascript / ajax

<script type = "text/javascript">
$(document).ready(function(){

    $(document).on('click', '.like', function(){
        var id=$(this).val();
        var $this = $(this);
        $this.toggleClass('like');
        if($this.hasClass('like')){
            $this.text('Like');
        } else {
            $this.text('Unlike');
            $this.addClass("unlike");
        }
            $.ajax({
                type: "POST",
                url: "like.php",
                data: {
                    id: id,
                    like: 1,
                },
                success: function(){
                    showLike(id);
                }
            });
    });

    $(document).on('click', '.unlike', function(){
        var id=$(this).val();
        var $this = $(this);
        $this.toggleClass('unlike');
        if($this.hasClass('unlike')){
            $this.text('Unlike');
        } else {
            $this.text('Like');
            $this.addClass("like");
        }
            $.ajax({
                type: "POST",
                url: "like.php",
                data: {
                    id: id,
                    like: 1,
                },
                success: function(){
                    showLike(id);
                }
            });
    });

});

function showLike(id){
    $.ajax({
        url: 'show_like.php',
        type: 'POST',
        async: false,
        data:{
            id: id,
            showlike: 1
        },
        success: function(response){
            $('#show_like'+id).html(response);

        }
    });
}

like.php

<?php
session_start();
ERROR_REPORTING(E_ALL & ~E_NOTICE);
include('php/connect.php');
include('php/function.php');
if (isset($_COOKIE['remember']) && $userid!==null) {
    getcookie();
}
if (isset($_POST['like'])){

    $id = $_POST['id'];

    $sql = "SELECT * FROM `like` WHERE postid = :postid AND userid = :userid";

    if($query = $pdo->prepare($sql)){
        $query->bindValue(':postid', $id);
        $query->bindValue(':userid', $userid);
        $query->execute();
        $nlikes = $query->rowCount();
    }


    if($nlikes>0) {
        $stmt2 = $pdo->prepare("DELETE FROM like WHERE userid=:userid AND postid=:bid");
        $stmt2->bindValue(':userid', $userid);
        $stmt2->bindValue(':bid', $id);
        $stmt2->execute();
    } else{

        $stmt3 = $pdo->prepare("INSERT INTO like (userid,postid) VALUES (:userid, :postid)");
        $stmt3->bindValue(':userid', $userid);
        $stmt3->bindValue(':postid', $id);
        $stmt3->execute();
    }

}
?>

show_like.php

<?php
session_start();
ERROR_REPORTING(E_ALL & ~E_NOTICE);
include('php/connect.php');
include('php/function.php');
if (isset($_POST['showlike'])){
    $id = $_POST['id'];

    $sql = "SELECT count(*) FROM `like` WHERE postid = :postid";

    if($query = $pdo->prepare($sql)){
        $query->bindValue(':postid', $id);
        $query->execute();
        $nlikes = $query->fetchColumn();
        echo $nlikes;
    }
}
?>

1 个答案:

答案 0 :(得分:1)

仅通过将数据库表的名称从 like 更改为 likes 即可解决 因为 like 是mysql的保留字。