【问题标题】:Multiple Form checkbox results used in mysqli selectmysqli select中使用的多表单复选框结果
【发布时间】:2016-03-02 13:21:48
【问题描述】:

我想将复选框与我的 SQL 选择语句集成,但我不确定如何将数组转换为可用的选择。

    <form method="post">
    <input name="searchterm" type="search" placeholder="Enter Search Terms Here" />
    <input name="searchbtn" type="submit" value="Karaoke Search" />
    <input name="checkbx[]" type="checkbox" checked value="Chartbuster" />
      <label>Chartbuster</label>
    <input name="checkbx[]" type="checkbox" checked value="Sound Choice" />
      <label>Sound Choice</label>
    <input name="checkbx[]" type="checkbox" checked value="DKKaraoke" />
      <label>DKKaraoke</label>
    <input name="checkbx[]" type="checkbox" checked value="Sunfly" />
      <label>Sunfly</label>
    <input name="checkbx[]" type="checkbox" checked value="Karaoke Hits" />
      <label>Karaoke Hits</label>
    <?php
      if(isset($_POST['searchterm']) and ($_POST['searchterm']!="")) {
        $searchterm=$_POST['searchterm'];
        $checkbx=$_POST['checkbx'];
        $searchresults=$db->query("SELECT *
                                   FROM 1KaraokeDJ
                                   WHERE Artist LIKE '%$searchterm%'
                                   GROUP BY Artist,
                                            Title,
                                            Brand  
                                   ORDER BY Artist,
                                            Title,
                                            Disc LIMIT 100");
      }
    ...
    ?>
     ...
</form>

基本上,将搜索限制在所选复选框之一中的品牌位置

【问题讨论】:

  • 只使用mysql IN() 语句,如 Brand IN (join(',', $checkbx)) link (stackoverflow.com/questions/907806/…)
  • SELECT * FROM 1KaraokeDJ WHERE Artist LIKE '%$searchterm%' AND Brand IN (join(',',$checkbx)) GROUP BY Artist, Title, Brand ORDER BY Artist, Title, Disc LIMIT 100 - 不产生任何结果
  • $checkbx = join(',',$checkbx); plus AND Brand IN $checkbx 也不会产生任何结果 - s

标签: php checkbox mysqli


【解决方案1】:

您可以使用它(尽管如果您担心应用程序的安全性,这不是最好的方法)

$checkbx = join(',',$checkbx);

然后这个变量应该适合你的查询。

另外请去掉圆括号,它们不是必需的:

$searchterm=($_POST['searchterm']);
$checkbx=($_POST['checkbx']);

变成

$searchterm=$_POST['searchterm'];
$checkbx=$_POST['checkbx'];

【讨论】:

  • 所以基本上你将数组转换为与“,”组合的字符串。我明白了,现在 sql 选择应该有 Brand = $checkbx?但不,那是行不通的……
  • 这种从数组到字符串的转换非常棒,但是在选择中添加 AND Brand IN $checkbx 不会产生任何结果。
【解决方案2】:

在头疼之后,我偶然发现了一些类似问题中没有添加到任何答案中的东西......

Select IN ('a','b','c'...) 需要在每个文本选项周围加上“'”。在所有示例中,它都假定为数字!

$checkbx = join(',',$checkbx);起初不起作用,因为数组中的每个文本项在加入之前都必须有“'”。

因此,我的解决方案(可能是更好的解决方案)是在我的每个复选框值中添加“'”:

<input name="checkbx[]" type="checkbox" value="'Chartbuster'" />
  <label>Chartbuster</label>
<input name="checkbx[]" type="checkbox" value="'Sound Choice'" />
  <label>Sound Choice</label>
<input name="checkbx[]" type="checkbox" value="'DKKaraoke'" />
  <label>DKKaraoke</label>
<input name="checkbx[]" type="checkbox" value="'Sunfly'" />
  <label>Sunfly</label>
<input name="checkbx[]" type="checkbox" value="'Karaoke Hits'" />
  <label>Karaoke Hits</label>
<?php
  if(isset($_POST['searchterm']) and ($_POST['searchterm']!="") and isset($_POST['checkbx'])) {
    $searchterm=$_POST['searchterm'];
    $checkbx=join(',',$_POST['checkbx']);
    $searchresults=$db->query("SELECT *
                               FROM 1KaraokeDJ
                               WHERE Artist LIKE '%$searchterm%'
                               AND Brand IN ($checkbx)
                               GROUP BY Artist,
                                        Title,
                                        Brand  
                               ORDER BY Artist,
                                        Title,
                                        Disc
                               LIMIT 100");
      }
    ...
    ?>
     ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2021-06-08
    • 2015-07-26
    • 2016-11-12
    • 2015-01-27
    • 1970-01-01
    相关资源
    最近更新 更多