vendredi 24 janvier 2020

How do I get a checkbox to show checked based on database value [duplicate]

I have read many of the other questions and answers on the site, but I have not been able to make this work. I have a page that is populated based on info from the database. I want to have check boxes that show checked if there is a 1 in the input field. I am sure that I am missing something (probably something simple), but I can not figure out what it is. Any help would be appreciated.

                $result = mysqli_query($mysqli, "SELECT id, firstname AS 'First Name', lastname AS 'Last Name', username AS 'Username', admin AS 'Administrator', manager AS 'Manager'
                                             FROM users
                                             ORDER BY lastname");           

            if (!$result) {
                die("Query to show fields from table failed");
            }

            $fields_num = mysqli_num_fields($result);

            echo "<h3>Admin and Manager User Report</h3>";
            echo "<table id='customers'>";
                // printing table headers
                echo "<th align='center'>First Name</th>";
                echo "<th align='center'>Last Name</th>";
                echo "<th align='center'>Username</th>";
                echo "<th align='center'>Administrator</th>";
                echo "<th align='center'>Manager</th>";

                echo "</tr>\n";
                // printing table rows
                while($row = mysqli_fetch_array($result)){
                    echo "<tr><form action=smupdate.php method=post>";
                    echo "<td align='center'><input type=text name=firstname value='".$row['First Name']."'></td>";
                    echo "<td align='center'><input type=text name=lastname value='".$row['Last Name']."'></td>";
                    echo "<td align='center'><input type=text name=username value='".$row['Username']."'></td>";
                    echo "<td align='center'><input type='checkbox' name='admin' value= <?php if ('".$row['Manager']."' == 1) {echo checked = 'checked';} ?> </td>";
                    echo "<td align='center'><input type=checkbox name=manager value= <?php if (('".$row['Manager']."' == 1) {echo checked = 'checked';} ?></td>";
                    echo "<input type=hidden name=id value='".$row['id']."'>";
                    echo "<td align='center'><input type=submit name=allupdate value=Update Record></td>";
                    echo "</form></tr>\n";
                }
                echo "</table>";
            mysqli_free_result($result);



Aucun commentaire:

Enregistrer un commentaire