javascript - 组织的用户限制

标签 javascript php mysql

我正在与组织一起制作一个网站。您可以请求成为组织的一部分。但该组织必须将用户限制为 500 人。它不会给出错误,也不会进入我的函数。通常它只需要检查 current_users 是否小于 500。然后该函数必须更新用户表和组织。

这是我迄今为止尝试过的:

<?php
 require '../conn.php';
 include '../session.php';
 $count = "SELECT `current_users` FROM `organisations` WHERE `org_id` = 
 '".$_POST['org']."'";
 $result = mysqli_query($conn, $count); // your query execution
 $row = mysqli_fetch_array($result);

if($row['current_users'] < 500){
    $this->requestUserCreate()
} else {
    throw new Exception('Limit reached')
}

function requestUserCreate() {
$q = "UPDATE `organisations` SET `current_users` = `current_users` + 1 WHERE 
`org_id`='".$_POST['org']."'";
$sql = "UPDATE `users` SET `active`= 1, `type` = 0 WHERE `user_id` = '" . 
$_POST['user'] . "'";
if ($conn->query($sql) && $conn->query($q) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}  
}
?>

这是我的组织表格

CREATE TABLE `organisations` (
`org_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`path` varchar(100) NOT NULL,
`type` int(1) NOT NULL,
`branche` varchar(50) NOT NULL,
`niveau` int(11) DEFAULT NULL,
`current_users` int(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

这是给用户的:

CREATE TABLE `users` (
`user_id` int(5) NOT NULL,
`fnln` varchar(100) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`org_id` int(5) DEFAULT NULL,
`part_id` int(11) NOT NULL DEFAULT '0',
`type` int(1) NOT NULL,
`active` int(11) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

我的 JavaScript

function approveOne(user, org) {
swal({
    title: 'Are you sure to approve this users?',
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Approve user'
}).then(function () {
    $.ajax({
        url: "functions/save/approveUser.php",
        type: "POST",
        data: {user, org},
        success: function (data) {
            swal(
                'Approved!',
                'You have approved the selected user.',
                'Success'
            );
            $("#newUsers").hide(250);
        },
        error: function (jXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
});
}

这是我的 html

<!doctype html>
<html lang="en">
<head>
<?php include 'include/header.php'; ?>
<title>Student Concepts | Users</title>
</head>
<?php
if (isset($_SESSION['type'])) {
if ($_SESSION['type'] != '1') {
    header('Location: index');
    die();
 }
}

function getUsers($orgID, $type)
{
require 'functions/conn.php';
$sql = "SELECT users.*, parts.* FROM users INNER JOIN parts ON users.part_id 
= parts.part_id WHERE users.org_id = '" . $orgID . "' AND users.active = '" 
. $type . "';";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<tr id="' . $row['user_id'] . '">';
        echo '  <td>' . $row['fnln'] . '</td>';
        echo '  <td>' . $row['username'] . '</td>';
        echo '  <td>' . $row['name'] . '</td>';
        echo '  <td>';
        if ($row['active'] == 0  || $row['active'] == 2) {
            echo '      <button class="button button-green" 
        onclick="approveOne('.$row['user_id'].', 
        '.$_SESSION['org_id'].')">Approve</button>';
        }
        echo '      <button class="button button-red" 
        onclick="deleteFromRelation(' . $row['user_id'] . 
        ');">Delete</button><br/>';
        echo '  </td>';
        echo '</tr>';
    }
}
}

?>

最佳答案

您需要解析查询结果。另外, if if ($count > 500) 中有一个逻辑错误。应该是if ($count < 500)因为您想在少于 500 时添加

$result = mysqli_query($conn, $count);
if ($result !== false) {
    $totalRows = mysqli_num_rows($result);
    if ($totalRows < 500) {
        $this->requestUserCreate();
    } else {
       throw new Exception('Limit reached');
    }
} else {
    echo "Error: " . $count . "<br>" . mysqli_error($conn);
}

现在,优化将重新进行查询,以使用 count() 函数而不是 select

关于javascript - 组织的用户限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54881538/

相关文章:

javascript - 谷歌AdSense广告未显示

php - 如何使用 PHP 和 MySQL 高效地对大型数据集进行分页?

php - Laravel 5 中如何获取一篇文章的 user_id ?

mysql - 在mysql中:how to display all column with distinct for one column

PHP:使用 optgroup 进行动态下拉

javascript - Slick JS 垂直,两个元素在同一行

javascript - 如何获取拖动开始时的父级 ID?用户界面

javascript - 更改 for 循环以在 javascript 中使用 forEach

php - 我想使用 php 在下拉菜单中的给定条件下访问 mysql 数据库表

MySQL ON DUPLICATE KEY UPDATE with JOIN for multiple rows insert in single query