0

I am creating a table with content fed from a table in a MySQL Database. One of the fields on the table is a checkbox form that adds roles to the user. I got it to work, but with the description being hardcoded. Since a new role might get added, I want to show it as a new checkbox option.

Clarification: the value of 1 sets the role as Admin, 2 as Subscriber...

The problem is the message: Notice: Array to string conversion in /var/www/html/wordpress/multi_users/user_table.php on line 110

<?php
$records_roles = mysqli_query($connect, "SELECT role_name FROM tbl_roles");
$roles = array();
$count = 0;
while ($course_roles = mysqli_fetch_assoc($records_roles)){
    $roles []= $course_roles;
    $count++;
} 
?>

<!DOCTYPE html>
<html lang="en">
<html>
<head>
    <title>Modify users</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="jquery-tabledit-1.2.3/jquery.tabledit.min.js"></script>
</head>

<body>
<div class="container">
    <br />
    <br />
    <br />
    <div class="table-responsive">
        <h3 align="center">Modifying Users</h3><br />
        <table id="editable_table" class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Approval</th>
                <th>Roles</th>
            </tr>
            </thead>
            <tbody>
            <?php
            while($row = mysqli_fetch_array($result))
            {
                echo '
              <tr>
               <td>'.$row["user_id"].'</td>
               <td>'.$row["first_name"].'</td>
               <td>'.$row["last_name"].'</td>
               <td>'.$row["email"].'</td>
               <td>
                    <form method="post">
                        <input type="hidden" name="user_id" value='.$row["user_id"].'>

                        <select name="admin_approved">
                            <option disabled selected value>--Select an option--</option>
                            <option value="1">Approved</option>
                            <option value="0">Dissaproved</option>
                        </select>
                        <button type="submit" name="selectSubmit" >Submit</button>
                    </form>
               </td>
               
               <td>
                    <form method="post">
                        Select user roles<br/>
                        <input type="hidden" name="user_id" value='.$row["user_id"].'>
               ';


                for ($a = 1; $a <= $count ; $a++){
                    echo '<input type="checkbox" name="techno[]" value="$a" />' .$roles[($a-1)]. "<br>";
                }// line 110

                echo '
                    </form>
                    </td>
                    </tr>
                ';
            }
            ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

1

Your issue stems from here:

while ($course_roles = mysqli_fetch_assoc($records_roles)){
    $roles []= $course_roles;
    $count++;
} 

$course_roles is an array of column values, even though you're only retrieving one column, so $roles really looks like

[
    ['role_name' => 'first_role_name'],
    ['role_name' => 'second_role_name']
]

What you need to do is get just the one column in that loop

while ($course_roles = mysqli_fetch_assoc($records_roles)){
    $roles []= $course_roles['role_name'];
    $count++;
} 
aynber
  • 22,380
  • 8
  • 50
  • 63
  • Thank you @aynber, that almost got everything to work. The value sent from the form is incorrect. It gets sent as ```value="$a"``` and not as an integer –  Oct 15 '20 at 18:49
  • That's because you're using single quotes around the full string, which will not parse the variable. If you switch out the quotes for double quotes (and either change the internal quotes to single quotes or escape them), then the variable will be evaluated. - https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – aynber Oct 15 '20 at 18:50