1

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>
4
  • In your second block of code, the HTML in the if else section is not inside an echo statement and you end the php code prior to it. What about encasing all of that inside a php tag and using echo? Commented Aug 11, 2018 at 19:44
  • 2
    Richard, all your inputs have the same name and same id. Try making each id and name of the form unique and see what happens. Commented Aug 11, 2018 at 19:56
  • Hello, Thanks for the response. Correct Chris, but it displays it fine. Its just easier for me to make the javascript correct (To call the form). Rafeal i tried to make the ID unique and this didn't work out. When i change the name im unable to call the PHP part. Commented Aug 11, 2018 at 22:19
  • See this answer stackoverflow.com/questions/2309801/… Commented Aug 11, 2018 at 23:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.