我試圖創建一個修改用戶表單,如果用戶是管理員或者他們是活動的,它有兩個複選框。PHP複選框沒有填充陣列
我已經得到它來讀取數據庫並相應地檢查複選框,但是當我嘗試從用戶窗體獲取值更新時,數據庫我的PHP只返回一個值。
<?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>
正在從陣列useractive打印的值目前只是:
Array ([0] => Yes)
而且,我非常想要的是:
Array ([0] => Yes, [1] => No, [2] => Yes)
給我看HTML部分也 –
考慮將複選框的值改爲記錄的「id」。即'name ='admin []'value =「<?= $ row ['id]?>」...'。這樣,用戶檢查時就可以輕鬆找到並更新記錄。嗯,如果用戶取消選中該選項,它將不會返回?一種方式:有一個隱藏的字段叫做:'name ='admin_id []'value =「<?= $ row ['id]?>'這會一直進來,可以使用,如果複選框丟失,未安裝「。 –