I'm having a very random problem with my PHP script. I'm displaying a few checkboxes to turn on and turn off my lights with the Raspberry Pi. I'm not new to PHP but i'm new with using checkboxes and setting the value's of it.
The problem is that when i select the checkbox the PHP script selects random value one of the checkboxes and not the one i selected.
My code is currently without SQL protection, i have to find the problem first..
Head of my code:
<?php
if(isset($_POST['lamp'])){
$lamp = $_POST['lamp'];
echo $lamp;
$statusSQL = mysqli_query($connect, "SELECT * FROM lamps WHERE name='$lamp' LIMIT 1");
$statusFetch = mysqli_fetch_array($statusSQL);
$lampStatus = $statusFetch['status'];
$lampID = $statusFetch['id'];
if ($lampStatus){
$sql = "UPDATE lamps SET status='0' WHERE id='$lampID'";
if ($connect->query($sql) === TRUE) {
echo "Lamp is off";
}
}
else{
$sql = "UPDATE lamps SET status='1' WHERE id='$lampID'";
if ($connect->query($sql) === TRUE) {
echo "Lamp is on";
}
}
}
?>
Getting checkboxes
<form id="checkForm" method="post" action="dashboard.php">
<?php
$sql = "SELECT * FROM lamps";
$result = mysqli_query($connect, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<div class='row'>";
if ($row["status"]){
?>
<div class='col-xs-4'><label class='switch'>
<input id="lamp" name="lamp" value="<?php echo $row["name"]; ?>" onclick="document.getElementById('checkForm').submit()" type='checkbox' checked>
<span class='slider round'></span>
</label></div><br>
<?
}
else{
?>
<div class='col-xs-4'><label class='switch'>
<input id="lamp" name="lamp" value="<?php echo $row["name"]; ?>" onclick="document.getElementById('checkForm').submit()" type='checkbox'>
<span class='slider round'></span>
</label></div><br><?
}
echo "</div>";
}
} else {
echo "No lamp active in database";
}
?>
</form>