0

How to fix the problem Notice:

Undefined variable: b in C:\wamp64\www\MyFolder\version1.1\fms\build\english\equipment.php on line 904

Warning: in_array() expects parameter 2 to be array, null given in C:\wamp64\www\MyFolder\version1.1\fms\build\english\equipment.php on line 904

<div class="form-group">
    <label class="custom-control custom-checkbox" style="margin-left: 10px; margin-right: 35px;">
        <input type="checkbox" name="equipment[]" value="Tv" class="custom-control-input" 
<?php 
if (isset($_POST["equipment_id"]))
    {
    $id = $_POST['equipment_id'];
    $query = mysqli_query("SELECT * FROM equipment WHERE id='$id'");
    $row = mysqli_fetch_array($query);
    $a = $row["equipment"];
    $b = explode(",", $a);
    print_r($b);
    }

if (in_array("Tv", $b))
    {
    echo "checked";
    }

?> >
        <span class="custom-control-indicator"></span>
        <img src="img/equipments/tv-icon.png" width="25px" height="25px">
        <span class="custom-control-description">TVs</span>
    </label>
    <label class="custom-control custom-checkbox" style="margin-right: 35px;">
        <input type="checkbox" name="equipment[]" value="Phone" class="custom-control-input">
        <span class="custom-control-indicator"></span>
        <img src="img/equipments/phone-icon.png" width="25px" height="25px">
        <span class="custom-control-description">Phone</span>
    </label>
    <label class="custom-control custom-checkbox" style="margin-right: 35px;">
        <input type="checkbox" name="equipment[]" value="Fan" class="custom-control-input">
        <span class="custom-control-indicator"></span>
        <img src="img/equipments/fan-icon.png" width="25px" height="25px">
        <span class="custom-control-description">Fan</span>
    </label>
    <label class="custom-control custom-checkbox" style="margin-right: 35px;">
        <input type="checkbox" name="equipment[]" value="Mirror" class="custom-control-input">
        <span class="custom-control-indicator"></span>
        <img src="img/equipments/mirror-icon.png" width="25px" height="25px">
        <span class="custom-control-description">Mirrors</span>
    </label>
</div>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
vibol
  • 7
  • 3

2 Answers2

1

As per my comment, place the in_array() condition in the isset() condition. The reason for the undefined variable is that $b only exists in the isset() condition. So when you leave the isset() condition and try to access $b, it throws the errors

if (isset()){
#your isset() code

    if(in_array()){
#in_array() code

    } #place in_array() inside isset()
} # end isset()

As a tip, always make a habit of defining your variables before use.. And lastly please use prepared statements for your queries..

Rotimi
  • 4,783
  • 4
  • 18
  • 27
1

Top of the php code please declare the array $b

 $b=array();

in your code you write a if condition and if it not satisfied then $b variable is undefined. So before the condition please declare the array like.

$b=array();
if (isset($_POST["equipment_id"])) {
//your code        
}

Thank you

Ganesh G Nath
  • 236
  • 1
  • 4