如何限制最多20个用户选择一个包

how to make a limit 20 users maximum to choose one package

这是我的代码,负责将选择的每个包名称分配给它 特定用户名我的想法是添加另一个查询,例如 query = select 来自username = 'package_name' 的userName; 如果受影响的行是 => 20 用户不应该能够执行此查询 选择这个包 否则 userName 将分配给该包名称

<?php
include("session.php");
include("connection.php");

$Pname= filter_input(INPUT_GET,'Pname');
$user_select = $_SESSION['login_user'];

$query='update users set package_name=? where userName=?';
  
$stmt=mysqli_stmt_init($conn);

mysqli_stmt_prepare($stmt,$query) or exit('Query Error.'. mysqli_stmt_errno($stmt));

@mysqli_stmt_bind_param($stmt,'ss',$Pname,$user_select) or exit('Bind Param Error.'); 

mysqli_stmt_execute($stmt) or exit('Query Execution failed.'. mysqli_stmt_errno($stmt));


if (mysqli_stmt_affected_rows($stmt)>0) echo "package chose successfully.";

mysqli_stmt_close($stmt);


mysqli_close($conn);
?>

使用触发器:

CREATE TRIGGER tr_bi_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN 
    IF 20 <= (SELECT COUNT(*)
              FROM users
              WHERE package_name = NEW.package_name
                AND userName <> NEW.userName) THEN
        SIGNAL SQLSTATE = '45000'
            SET MESSAGE_TEXT = 'The limit for this package reached.';
    END IF;
END

和更新前的相同触发器。