Php 单击like按钮时显示错误消息

Php 单击like按钮时显示错误消息,php,mysql,Php,Mysql,您好,我想使用PHP和MySQL创建一个类似的系统。单击“类似”按钮时,我也会在数据库中插入数据,但插入了一个错误数据库值,但类似值为0,没有增量,出现未定义的错误。有人能帮我解决这个问题吗 There is my Like button code : <?php //// work with like box $get_likes = mysqli_query($con,"SELECT * FROM `likes`"); if (mysqli_nu

您好,我想使用PHP和MySQL创建一个类似的系统。单击“类似”按钮时,我也会在数据库中插入数据,但插入了一个错误数据库值,但类似值为0,没有增量,出现未定义的错误。有人能帮我解决这个问题吗

There is my Like button code :

<?php 
     //// work with like box 
      $get_likes = mysqli_query($con,"SELECT * FROM `likes`");
      if (mysqli_num_rows($get_likes)===1) {

            $get = mysqli_fetch_assoc($get_likes);
           // $uid = $get['uid'];
            $total_likes = $get['total_likes'];
            //echo $uid;
            $total_likes =   $total_likes  + 1;
            //echo $total_likes++; 
        }   

    if (isset($_POST['likebutton_'])) {
      $like = mysqli_query($con,"UPDATE `likes` SET `total_likes`  = '$total_likes'") or die(mysqli_error($con));

    //$insert_Data = mysqli_query($con,"INSERT INTO `likes` (`uid`) VALUES('$username')") or die(mysqli_error($ocn));
     header("Location:home.php");

    }

    else 
    {
      echo "Error";
    }
    ?>
    this code work fine without insert Data
    There is My liked with Data Insertd Code 
     <?php 
     ////work with like box 
      $get_likes = mysqli_query($con,"SELECT * FROM `likes`");
      if (mysqli_num_rows($get_likes)===1) {

            $get = mysqli_fetch_assoc($get_likes);
           // $uid = $get['uid'];
            $total_likes = $get['total_likes'];
            //echo $uid;
            $total_likes =   $total_likes  + 1;
            //echo $total_likes++; 
        }   

    if (isset($_POST['likebutton_'])) {
      $like = mysqli_query($con,"UPDATE `likes` SET `total_likes`  = '$total_likes'") or die(mysqli_error($con));

    $insert_Data = mysqli_query($con,"INSERT INTO `likes` (`uid`) VALUES('$username')") or die(mysqli_error($ocn));
     header("Location:home.php");

    }

    else 
    {
      echo "Error";
    }
    ?>
    this is output i want to display my font-end page <?php echo $total_likes ;?> but it occur error

    The error is Undefined Variable 
I also try $total_likes=""; 
as global but still not work
有我喜欢的按钮代码:

您的代码受到竞争条件的影响。您应该这样做:

INSERT INTO likes (uid, total_likes) VALUES (?, 1)
  ON DUPLICATE KEY SET total_likes=total_likes+1
使用
bind_param
将占位符值设置为UID


请注意,在一个查询中,您将所有喜欢的总数设置为+1。这是一个巨大的错误。

您可以为表单、数据库和要显示结果的前端页面添加代码吗?您不需要获得
likes
只需使用
mysql
更新当前行+1即可。您可以使用此代码进行SQL注入。你不应该传递一些ID,这样你就不会更新每一条记录了吗?我猜你遇到的问题是
mysqli\u num\u rows($get\u likes)
不等于
1
。如果计数为1,则仅分配
$total_likes
。警告:使用
mysqli
时,应使用和将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将
$\u POST
$\u GET
数据直接放入查询,如果有人试图利用您的错误,这可能非常有害。