【问题标题】:how to find range inside checkbox如何在复选框内查找范围
【发布时间】:2017-08-29 17:10:48
【问题描述】:

我的表 accounts 中有一个名为 weight 的列。我需要从该列中获取值,例如,

当用户点击体重在40-100之间时,需要显示所有体重在40-100之间的人。

这是我的html代码,

<div class="float-right span35">
    <form class="list-wrapper search-form" method="get" action="{{CONFIG_SITE_URL}}/index.php">
        <div class="list-header">
            Talent Search
        </div>
                <h4 style="margin-left: 10px; color: gray;">Weight</h4>
                <fieldset id="weight">
                    <input type="checkbox" name="weight" value="1" {{WEIGHT_1}}/>Below 40<br>
                    <input type="checkbox" name="weight" value="2" {{WEIGHT_2}}/>40-70<br>
                    <input type="checkbox" name="weight" value="3" {{WEIGHT_3}}/>70-100<br>
                    <input type="checkbox" name="weight" value="4" {{WEIGHT_4}}/>Above 100<br>
                </fieldset>
                <br/>
                <input style="margin-left: 10px" type="submit" name="submit" value="search"/><br><br>
                <input type="hidden" name="tab1" value="search">
        </form>
</div>

这是我的php代码,

if(isset($_GET['submit'])){


    $weight = ($_GET['weight'])? $escapeObj->stringEscape($_GET['weight']): NULL;

    $sql = "SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL ";

    if(is_numeric($weight) && $weight != NULL){
        $sql .= "AND `weight` IN (".$weight.")"; 
        $is_weight = true;
    }

    $sql .= " ORDER BY `id` ASC";

    }
    if($is_weight){
        $themeData['weight_'.$weight] = 'checked';
    }

    $query = $conn->query($sql);
    if($query->num_rows > 0){
            while($fetch = $query->fetch_array(MYSQLI_ASSOC)){
                $get[] = $fetch['id'];
            }

            $i = 0;
            $listResults = '';

            $themeData['page_title'] = "Advanced Search - ".$lang['search_result_header_label']; 
            foreach($get as $row){
                $timelineObj = new \SocialKit\User();
                $timelineObj->setId($row);
                $timeline = $timelineObj->getRows();

                $themeData['list_search_id'] = $timeline['id'];
                $themeData['list_search_url']   = $timeline['url'];
                $themeData['list_search_username'] = $timeline['username'];
                $themeData['list_search_name'] = $timeline['name'];
                $themeData['list_search_thumbnail_url'] = $timeline['thumbnail_url'];

                $themeData['list_search_button'] = $timelineObj->getFollowButton();

                $listResults .= \SocialKit\UI::view('search/list-each');
                $i++;
            }
            $themeData['list_search_results'] = $listResults; 

    }

}

此代码几乎适用于表格中的单个重量。但是我需要在代码中提到的复选框值之间创建一个范围。我需要更新代码吗?

【问题讨论】:

  • 如果您要添加范围,则需要在查询中进行更改并添加类似“where weight >=min && weight
  • 作为 php 和 mysql 的新手,我不知道如何清楚地做到这一点。你能帮我找到吗? @bdalina。
  • 什么值被传递给 php?这个1,2,3,4?每个都有最小值和最大值 1=40 到 100?
  • 传递存储在数据库表中的整数值
  • 不要通过字符串连接构建 SQL 查询。 SQL 注入攻击就是这样。

标签: php checkbox


【解决方案1】:

在php代码更改

if(is_numeric($weight) && $weight != NULL){
    $sql .= "AND `weight` IN (".$weight.")"; 
    $is_weight = true;
}

if(is_numeric($weight) && $weight != NULL){
    if($weight==1){
        $sql .= "AND weight<40";
    }
    if($weight==2){
        $sql .= "AND weight>=40 AND weight <=70";
    }
    if($weight==3){
        $sql .= "AND weight>70 AND weight <=100";
    }
    if($weight==4){
        $sql .= "AND weight>100";
    }
    $is_weight = true;
}

另外,我建议使用单选而不是复选框,就像使用复选框一样,如果您选择多个权重选项,则会丢失一个值。

【讨论】:

  • 你的权利,他必须使用收音机,并为 All 1,2,3,4 添加另一个选项,All 表示 0 -100 以上等。
  • /?weight=weight+>70+and+weight+ 我的网址变成了这样,但没有结果查看
  • 忽略之前给出的 HTML 建议。按照上面的建议更新 PHP 代码。
【解决方案2】:

试试这个,希望它有效,这样你的代码就不会发生大的变化

使用单选按钮

<input type="radio" name="weight" value="0" {{WEIGHT_1}}/>Below 40<br>
<input type="radio" name="weight" value="1" {{WEIGHT_2}}/>40-70<br>
<input type="radio" name="weight" value="2" {{WEIGHT_3}}/>70-100<br>
<input type="radio" name="weight" value="3" {{WEIGHT_4}}/>Above 100<br>

可以在php中添加变量数组

$arrayWeight = ["`weight` >= 0 && `weight` <=40", 
                "`weight` >= 40 && `weight` <=70", 
                "`weight` >=70 && `weight` <=100",
                "`weight` >= 100"];

//  $arrayWeight[$weight] = $arrayWeight[1];
//  $arrayWeight[1] = 'weight' >= 0 && 'weight' <=40    a string

在这一行添加做这个

if(is_numeric($weight) && $weight != NULL)
    {
        $sql .= "AND ".$arrayWeight[$weight].")";  //sample $arrayWeight[1] = 'weight' >= 0 && 'weight' <=40

        //" SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL AND 'weight' >= 0 && 'weight' <=40

        $is_weight = true;
    }

谨慎使用引号

【讨论】:

  • 我需要把这个 $arrayWeight = ["", //为什么为空意味着它会查询所有的重量 "'weight' >= 0 && 'weight' = 40 && 'weight' =70 && 'weight' = 100",]; // $arrayWeight[$weight] = $arrayWeight[1]; // $arrayWeight[1] = 'weight' >= 0 && 'weight'
  • 它是我添加的选项,您可以看到现在有 5 个选项,在 html 中如果不需要,可以将其删除,但输入中的值必须以 0 开头,因为数组从零开始
  • 当我使用 urs 更新我的代码时,我没有得到任何更改
  • 放在哪里?是在 PHP 页面中还是在 html 中
  • 请查看更新。我已经用 'weight'=weight 中的魔术引号替换了单引号,最后我删除了一个可能导致错误的逗号
猜你喜欢
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 2020-12-01
  • 2016-09-22
  • 2016-10-08
  • 1970-01-01
相关资源
最近更新 更多