Role query is just checking if user has any role so it will return always true (if user has at least 1 role) or always false (if user has no role).
Instead
$new_sql = "SELECT role_id from users_roles, users WHERE users_roles.user_id = '".$row["user_id"]."' GROUP BY users_roles.user_id";
You have to add a filter byrole_id
.
$new_sql = "SELECT role_id from users_roles, users WHERE users_roles.user_id = '".$row["user_id"]."' AND users_roles.role_id = '" . $a . "' GROUP BY users_roles.user_id";
Note: As sugested in the comments you should use parameterized queries to protect against sql injection attacks. Also you should escape variables before echoing to protect against cross-site scripting (xss) attacks.
Above change should fix your problem but I have some suggestions. Check below.
Suggestion 1:
You are assuming thatrole_id
is the same as$count
index.
If you don't have a sequential id in roles table that starts with 1 or by any chance the result order is changed then code will break.
$roles
array needs to track role ID. Subsequent code need to change accordingly.
Assuming columnid
in roles table:
$records_roles = mysqli_query($connect, "SELECT id, role_name FROM roles");
$roles = array();
$count = 0;
while ($course_roles = mysqli_fetch_assoc($records_roles)){
// Keep track of roles ID
$roles[] = [
'id' => $course_roles['id'],
'name' => $course_roles['role_name'],
];
$count++;
}
Suggestion 2: You are executing a database query to check for each role individually. This affects code performance.
Get all roles of the user in 1 query and then compare role id in the array.
Complete code with 2 suggestions will look like this
<?php
// Display user information on table
$query = "SELECT user_id, first_name, last_name, email FROM users ORDER BY user_id ASC";
$result = mysqli_query($connect, $query);
$records_roles = mysqli_query($connect, "SELECT id, role_name FROM roles");
$roles = array();
while ($course_roles = mysqli_fetch_assoc($records_roles)){
// Keep track of roles ID
$roles[] = [
'id' => $course_roles['id'],
'name' => $course_roles['role_name'],
];
}
while($row = mysqli_fetch_array($result))
{
echo "
<tr>
<td style='display:none'>".$row['user_id']."</td>
<td>".$row['first_name']."</td>
<td>".$row['last_name']."</td>
<td>".$row['email']."</td>
<td>
<form method='post'>
Select user roles<br/>
<input type='hidden' name='user_id' value=".$row["user_id"].">
";
// Get all user roles
$new_sql = "SELECT role_id FROM users_roles WHERE user_id = '".$row["user_id"]."' GROUP BY user_id";
$user_roles_result = mysqli_query($connect, $new_sql);
$user_roles = [];
while ($row_user_roles = mysqli_fetch_assoc($user_roles_result)){
// Put user roles in array
$user_roles[] = $row_user_roles['role_id'];
}
foreach ($roles as $role) {
if (in_array($role['id'], $user_roles)) {
echo "<input type='checkbox' checked name='techno[]' value='{$role['id']}' />" .$role['name']. "<br>";
} else {
echo "<input type='checkbox' name='techno[]' value='{$role['id']}' />" .$role['name']. "<br>";
}
}
echo "
<button class='btn btn-primary' type='submit' name='checkSubmit' >Submit</button>
</form>
</td>
";
echo "
</tr>
";
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.