如何解决问题注意:
未定义的变量:b in C:\ wamp64 \ www \ MyFolder \ version1.1 \ fms \ build \ english \ equipment.php on 第904行
警告:in_array()期望参数2为数组,null中给定为null C:\ wamp64 \ www \ MyFolder \ version1.1 \ fms \ build \ english \ equipment.php on 第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>
答案 0 :(得分:1)
根据我的评论,将in_array()
条件置于isset()
条件下。未定义变量的原因是$b
仅存在于isset()
条件中。因此,当您离开isset()
条件并尝试访问$b
时,会抛出错误
if (isset()){
#your isset() code
if(in_array()){
#in_array() code
} #place in_array() inside isset()
} # end isset()
作为提示,请务必养成在使用前定义变量的习惯..最后请使用预备语句进行查询..
答案 1 :(得分:1)
php代码的顶部请声明数组$ b
$b=array();
在您的代码中编写if条件,如果不满足则$ b变量未定义。所以在条件之前请声明数组。
$b=array();
if (isset($_POST["equipment_id"])) {
//your code
}
谢谢