-2
在PHP MYSQLI中存储具有不同值和名称的多个复选框时出现问题。我已经成功地用单列和单列,但是当存储3列时它失败了。在PHP中存储具有不同值和名称的多个复选框MYSQLI
处理过程:当某人选择department
,position
和location
并带有多个复选框时,它将根据id_user在每行和每列存储到db。
我的表:
id, id_usr, id_department, id_position, id_location
我想要的结果:
1 | 1 | 4 | 2 | 2
2 | 1 | 1 | 2 | 1
3 | 1 | 2 | 3 | 4
4 | 1 | 4 | 2 | 2
5 | 2 | 1 | 2 | 1
6 | 2 | 2 | 3 | 4
我的代码:
<div class="form-group"><label>Department</label><br>
<?php
$department = "SELECT * FROM loker_department ORDER BY nama_department";
$result = mysqli_query($conn, $department);
if (mysqli_num_rows($result) > 0)
{
while ($data = mysqli_fetch_array($result))
{
echo "<input type='checkbox' name='department[]' value='".$data['id_department']."'/> $data[nama_department]<br>";
}
}else{echo "No data";}
?>
</div>
<div class="form-group"><label>Position</label><br>
<?php
$position = "SELECT * FROM loker_position ORDER BY nama_position";
$result = mysqli_query($conn, $position);
if (mysqli_num_rows($result) > 0)
{
while ($data = mysqli_fetch_array($result))
{
echo "<input type='checkbox' name='position[]' value='".$data['id_position']."'/> $data[nama_position]<br>";
}
}else{echo "No data";}
?>
</div>
<div class="form-group"><label>Location</label><br>
<?php
$location = "SELECT * FROM loker_location ORDER BY nama_location";
$result = mysqli_query($conn, $location);
if (mysqli_num_rows($result) > 0)
{
while ($data = mysqli_fetch_array($result))
{
echo "<input type='checkbox' name='location[]' value='".$data['id_location']."'/> $data[nama_location]<br>";
}
}else{echo "No data";}
?>
</div>
$department = $_POST['department'];
$position = $_POST['position'];
$location = $_POST['location'];
foreach($department as $departments && $position as $positions && $location as $locations)
{
$create = "INSERT INTO loker_user_subsc (id_department,id_position,id_location) VALUES ('$departments','$positions','$locations')";
$result5 = mysqli_query($conn, $create);
}
任何帮助/ soluti ons将不胜感激。
错误,您在使用http://php.net/manual/en/function.error-reporting得到什么。 PHP和http://php.net/manual/en/mysqli.error.php? –
'foreach'获取一个数组中的所有值;你应该得到一个关于它的错误。看到这个问答http://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop –
@ Fred-ii-解析错误:语法错误,意外'&&'(T_BOOLEAN_OR),期待')'在 – azmi27