mercredi 4 novembre 2015

PHP Checkbox not filling array

I'm trying to create a modify users form, which has two checkboxes on for if a user is an Admin or if they are Active.

I've got it to read the database and check the check boxes accordingly but when I try to get the values from the userform to update, the database my PHP only returns one value. If anyone could help as to where I'm going wrong or as to what I'm missing or an alternative way it would be much appreciated.

<?php 
session_start(); 

include 'scripts/dbconnection.php';
include 'scripts/logoncheck.php';
include 'scripts/uservariables.php';

////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////Get operator number and name///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

$sql = "SELECT * from users";

$result = $mysqli->query($sql);

$displaytable = "<table>";
$displaytable = $displaytable."<thead><tr><th colspan='5'>Users!</th></tr>";
$displaytable = $displaytable."<tr><th>Number</th><th>Name</th><th>Admin</th><th>Active</th><th>Password</th></tr></thead><tbody>";

$user = array();

while($row = $result->fetch_assoc())
{
    $displaytable = $displaytable."<tr>";
    $displaytable = $displaytable."<td>".$row['opnum']."</td>";
    $displaytable = $displaytable."<td><input type='text' name='username[]' value='".$row['opname']."' required></td>";

    $admin = $row['admin'];

    if($admin == "Yes")
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='Yes' checked='checked'/></td>";
    }
    else
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='No'/></td>";
    }

    $active = $row['active'];

    if($active == "Yes")
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='Yes' checked='checked'/></td>";
    }
    else
    {
        $displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='No' /></td>";
    }
    $displaytable = $displaytable."<td><input type='checkbox' name='reset_password[]' /></td>";
    $displaytable = $displaytable."</tr>";
}

$displaytable = $displaytable."</tbody></table>";

$num_rows = mysqli_num_rows($result);


if(isset($_POST['submit']))
{
    $username = $_POST['username'];
    $useradmin = $_POST['admin'];
    $useractive = $_POST['active'];
    $resetpassword = $_POST['reset_password'];

    print_r($_POST['admin'][0]);
    print_r($_POST['admin'][1]);
    print_r($_POST['admin'][2]);

    for ( $i = 0; $i < $num_rows; $i++ ) 
    {
            echo "<br> User:".$username[$i]."<br>";
            if(isset($useradmin[$i]))
            {
                echo $username[$i]." is Admin <br>";
            }
            elseif(!isset($useradmin[$i]))
            {
                echo $username[$i]." is not Admin <br>";
            }

            if(isset($useractive[$i]))
            {
                echo $username[$i]." is Active <br>";
            }
            elseif(!isset($useractive[$i]))
            {
                echo $username[$i]." is not Active <br>";
            }           

            if(isset($resetpassword[$i]))
            {
                echo $username[$i]." reset password <br>";
            }
            elseif(!isset($resetpassword[$i]))
            {
                echo $username[$i]." not reset password <br>";
            }
    }
}

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <LINK href="home.css" rel="stylesheet" type="text/css">
        <title>Add Results</title>
    </head>
    <body>
        <div class="login-card">                
            <?php include 'scripts/consts/menu.php';?>
            <h1>Add Results!</h1>
            <br>
            <div class="results1">
                <form action="#" method="POST">
                    <?php echo $displaytable;?>
                    <input name="submit" value="Save" type="submit" class="login login-submit">
                </form>
            </div>
        </div>
    </body>
</html>

The values that are being printed from the array useractive currently is just:

Array ( [0] => Yes ) 

And, what I ideally want is:

Array ( [0] => Yes, [1] => No, [2] => Yes) 




Aucun commentaire:

Enregistrer un commentaire