单击“赞”按钮时显示错误消息

时间:2016-05-12 19:28:41

标签: 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_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

1 个答案:

答案 0 :(得分:0)

您的代码遇到竞争条件。你应该做的是这种模式:

INSERT INTO likes (uid, total_likes) VALUES (?, 1)
  ON DUPLICATE KEY SET total_likes=total_likes+1

使用bind_param将占位符值设置为UID的位置。

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