mysql数据值无缘无故增加

时间:2017-08-14 07:51:45

标签: php mysql telegram-bot

我为我的电报机器人编写此代码,以检查每个用户在组中发布的帖子数量

$trimmedID = trim($chatId,"-");
$chat_run = mysql_query("SELECT user_id FROM `$trimmedID`");
if(mysql_num_rows($chat_run) > 1) {
    $query_post = "SELECT `post` FROM `$trimmedID` WHERE `user_id`='$chatMID'";
    $query_post_run = mysql_query($query_post);
    if(mysql_num_rows($query_post_run) == 0)
    {
        $query="INSERT INTO `$trimmedID` VALUES('1','$chatMID','$firstname','$lastname','$username')";
        mysql_query($query);
    }else{
        $post = mysql_fetch_assoc($query_post_run);
        $count = $post['post']+1;
        $query="UPDATE `$trimmedID` SET `post`='$count' WHERE `user_id`='$chatMID'";
        mysql_query($query);
    }
}else{
    $create_TB = "CREATE TABLE `$trimmedID` (
    post int (100) NOT NULL,
    user_id int (20) NOT NULL PRIMARY KEY,
    firstname varchar (100) NOT NULL,
    lastname varchar (100) NOT NULL,
    username varchar (100) NOT NULL
    )";
    mysql_query($create_TB);
    $query="INSERT INTO `$trimmedID` VALUES('1','$chatMID','$firstname','$lastname','$username')";
    mysql_query($query);
}

首先检查表是否存在 然后检查用户是否存在 最后,如果用户存在,则在mysql中使用UPDATE增加其post值的数量

某些用户的帖子值自行增加,没有明确的理由。 我该如何解决这个问题?

0 个答案:

没有答案