组织的用户限制

时间:2019-02-26 08:50:06

标签: 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>';
    }
}
}

?>

1 个答案:

答案 0 :(得分:0)

您需要解析查询结果。另外,如果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

相关问题