javascript - 组织的用户限制
问题描述
我正在与组织建立一个网站。您可以申请成为组织的一员。但是这些组织必须获得 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 ($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
推荐阅读
- python - Beautiful Soup 在现有元素上返回 None
- android - 将字符串解析为日期和格式
- ios - 为什么请求 Alamofire 失败?
- javascript - 使用管道将数据从 msssql 流式传输到节点
- jpa-2.0 - 使用连接继承表时连接子表 [谓词]
- python - Kivy 标签文本开始更新太晚
- html - chrome 79 更新后页面被切断
- c++ - 将二进制转换为十进制我如何要求用户只输入二进制
- html - 将 2 个项目并排放置流体/响应(未知宽度)
- controller - 使用 clear() 方法清除 TextEditingController 会将焦点重新放在文本字段上