首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用爆炸性php编辑和更新复选框值

使用爆炸性php编辑和更新复选框值
EN

Stack Overflow用户
提问于 2017-11-24 05:11:02
回答 2查看 231关注 0票数 0

如何修复问题通知:

未定义变量: C:\wamp64\www\MyFolder\version1.1\fms\build\english\equipment.php中的B行904 警告: C:\wamp64\www\MyFolder\version1.1\fms\build\english\equipment.php ()要求参数2为数组,在第904行的in_array中为null

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2017-11-24 05:15:20

根据我的注释,将in_array()条件放在isset()条件中。未定义变量的原因是$b只存在于isset()条件中。因此,当您离开isset()条件并尝试访问$b时,它会抛出错误

代码语言:javascript
复制
if (isset()){
#your isset() code

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

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

作为一个提示,一定要养成在使用前定义变量的习惯。最后,请使用准备好的语句进行查询。

票数 1
EN

Stack Overflow用户

发布于 2017-11-24 05:25:28

在php代码顶部,请声明数组$b

代码语言:javascript
复制
 $b=array();

在您的代码中,您将编写一个if条件,如果它不满足,那么$b变量是未定义的。因此,在条件之前,请声明数组如下。

代码语言:javascript
复制
$b=array();
if (isset($_POST["equipment_id"])) {
//your code        
}

谢谢

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47466713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档