简体   繁体   English

似乎无法让我喜欢/不喜欢在PHP中工作

[英]Can't seem to get my like/dislike to work in PHP

My table is comment_likedislike. 我的表是comment_likedislike。 It has the comment_counterid, comment_counter, comment_id(which is from another table) fields. 它具有comment_counterid,comment_counter,comment_id(来自另一个表)字段。 And I have an url (LIKE) that when clicked would link to this code and get the comment_id and like_id. 我有一个网址(LIKE),单击该网址即可链接到此代码,并获取comment_id和like_id。

I want to do a count where if it is the first 'like', it would store a new comment_counter in the comment_likedislike table. 我想做一个计数,如果它是第一个“赞”,它将在comment_likedislike表中存储一个新的comment_counter。 But if there is already a 'like' for the comment in the table, it would just update the comment_counter to +1. 但是,如果表中的注释已经有一个“赞”,它只会将comment_counter更新为+1。

Problem: When I run this code, it doesn't UPDATE(1st statement) but INSERT(2nd if statement) no matter if there is a like for the comment or not. 问题:当我运行这段代码时,无论是否有类似的注释,它都不是UPDATE(第一个语句),而是INSERT(第二个if语句)。 I don't think the code is checking if the comment_id is in the table already. 我不认为代码正在检查表中是否已经有comment_id。

I am a novice php programmer. 我是一个PHP新手。

Thanks! 谢谢!

if (isset($_GET['comment_id']) && isset($_GET['like_id'])) {
    $query5="SELECT * FROM comment_likedislike ";
    $data5=mysqli_query ($dbc, $query5);
    while ($row5= mysqli_fetch_array($data5)){
        $comment_id2=$row5['comment_id'];   
    }

        if ($comment_id2 == $_GET['comment_id']){

            $counter=$row5['comment_counter'];
            $counter++;

            $query= "UPDATE comment_likedislike SET comment_counter ='$counter' WHERE comment_id= '".$_GET['comment_id']."' ";
            mysqli_query($dbc, $query);

        }
        if ($comment_id2 != $_GET['comment_id']) {
            $counter2=1;
            $query9 = "INSERT INTO comment_likedislike (comment_counter, comment_id) VALUES ('$counter2', '".$_GET['comment_id']."' )";
            mysqli_query($dbc, $query9);
        }

}

You should put a WHERE clause into your SQL query rather than grabbing the whole table and parsing it in PHP. 您应该在SQL查询中放入WHERE子句,而不要获取整个表并在PHP中进行解析。

if (isset($_GET['comment_id']) && isset($_GET['like_id'])) {
    $query5="SELECT * FROM comment_likedislike WHERE comment_id = '" . $_GET['comment_id'] . "'";
    $data5 = mysqli_query ($dbc, $query5);
    $row5 = mysqli_fetch_array($data5);

Then you can switch on the value of empty($row5). 然后,您可以打开empty($ row5)的值。

Your code is broken because $comment_id2 will always be the last comment id in the table. 您的代码已损坏,因为$ comment_id2将始终是表中的最后一个注释ID。 You pull the entire table, then set $comment_id2 to each 'comment_id' field in turn. 您拉出整个表,然后依次将$ comment_id2设置到每个“ comment_id”字段。

I'm trying to infer what you're trying to do based on your code. 我试图根据您的代码推断出您要做什么。 It looks like you're trying to increase a counter in a row corresponding to a given comment ID present in your GET parameters. 似乎您要尝试在与GET参数中存在的给定注释ID对应的行中增加一个计数器。 And if there is no row corresponding to that comment you want to create a new one. 如果没有与该注释相对应的行,则要创建一个新注释。

If that's what you're intending to do then you're going about it all wrong. 如果这是您打算要做的,那您就错了。 It's kind of difficult to explain how wrong your code is because I can't figure out what mindset you'd have to be in to come up with code like that. 很难解释您的代码有多错误,因为我无法弄清楚您要使用哪种代码来编写这样的代码。

First of all, you should put your check for the existence of the row in the SQL query, then you need to revise the structure of your if statements: 首先,应在SQL查询中检查是否存在该行,然后需要修改if语句的结构:

if (isset($_GET['comment_id']) && isset($_GET['like_id'])) {

    // Ignoring obvious SQL injection vulnerability for now
    $query5="SELECT * FROM comment_likedislike WHERE comment_id = '" .
      $_GET['comment_id'] . "'";

    $data5=mysqli_query ($dbc, $query5);
    if ($row5= mysqli_fetch_array($data5)){
      $counter=$row5['comment_counter'];
      $counter++;

      $query= "UPDATE comment_likedislike SET comment_counter ='$counter' WHERE comment_id= '".$_GET['comment_id']."' ";
      mysqli_query($dbc, $query);
    }
    else 
      $counter2=1;
      $query9 = "INSERT INTO comment_likedislike (comment_counter, comment_id) VALUES ('$counter2', '".$_GET['comment_id']."' )";
      mysqli_query($dbc, $query9);
    }    
}

I'd also recommend reading Best way to stop SQL Injection in PHP because you never want to be building queries the way you're doing it in your example. 我还建议您阅读停止PHP中SQL注入的最佳方法,因为您永远都不想在示例中以这种方式构建查询。 (Or mine, for that matter.) (或者是我的。)

Use an echo statement to see what values you're actually getting for $_GET['comment_id'] and $comment_id2 . 使用echo语句查看$_GET['comment_id']$comment_id2实际获得的值。 Since you're never hitting the update case, the two values must never be equivalent. 由于您永远都不会碰到更新案例,因此两个值绝不能相等。

There could be any number of reasons why they are not matching, from extra spaces in one or the other of the values (which you could trim out) to your HTML form containing a bug that causes the parameter never to be sent. 导致它们不匹配的原因可能有多种,从一个或另一个值的多余空格(可以修剪)到HTML表单(其中包含导致无法发送参数的错误)的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM