我有使用此代码生成的复选框:
while($row = $result->fetch_assoc()) {
echo "<input type='checkbox' name='branch[]' value='".$row['id']."'> ".$row['name']."<br>";
}
我需要在mysql数据库中插入每个复选框的值。我为每个复选框插入了代码,但只有$member_id
,$branch
始终插入为0.我做错了什么?
$sql = "SELECT id FROM members WHERE email='$email'";
$result = mysqli_query($mysqli,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$member_id = $row["id"];
$checked_arr = $_POST['branch'];
$branch_count = count($checked_arr);
for ($i = 0; $i < $branch_count; $i++) {
if ($insert_stmt_branch = $mysqli->prepare("INSERT INTO members_branch (member, branch) VALUES (?, ?)")){
$insert_stmt_branch->bind_param('ss', $member_id, $branch);
if (! $insert_stmt_branch->execute()) {
echo "ERROR: branch insert";
}
}
}
答案 0 :(得分:1)
您永远不会为$branch
答案 1 :(得分:1)
在你的for循环开始时,你忘了添加:
$branch = $checked_arr[$i];
此外,您无法验证通过电子邮件地址查询的查询是否返回值。如果未找到任何帐户,则$ member_id值将为空。