i have a problem when Storing Multiple Checkbox with Different Value and Name in PHP MYSQLI. I already succeed with single column and row, but when storing 3 column i failed.
The process: when someone choose department, position and location with multiple checkbox, it will store to db on each row and column based on id_user.
My table:
id, id_usr, id_department, id_position, id_location
The result i want:
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
My code:
<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);
}
Any help/ solutions will be so appreciate, thank you
Aucun commentaire:
Enregistrer un commentaire