如何查询是否选中了多个复选框?

时间:2015-01-18 14:06:01

标签: php mysql

我正在使用php编写生成报告代码,如果我在表单中选中了多个复选框,则会出现问题。

这是我的示例HTML代码。

<input type="check" name="permit" value="Locational Clearance"/>Locational Clearance
    <input type="check" name="permit"  value="PALC"/>PALC
    <input type="check" name="permit"  value="DP"/>DP
    <input type="check" name="permit"  value="AP"/>AP

这是我的查询。

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";



    $year = $_POST['year'];
    $permit = $_POST['permit'];
    $range_to = $_POST['range_to'];
    $range_from = $_POST['range_from'];
    $comma_separated_permits = implode(",", $permit);
    // Create connection
    $conn = mysql_connect($servername, $username, $password);
    // Check connection
    if(! $conn )
    if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT  permit_type, count(id) as 'no',DATE_FORMAT(date,'%M %Y') as 'month', sum(total_amount) as 'amount'     
        FROM payments
        WHERE date like '%$year%' and permit_type IN ('$comma_separated_permits')
        group by DATE_FORMAT(date,'%M')
        order by DATE_FORMAT(date,'%m')";

mysql_select_db('cpdo_db');
$result = mysql_query( $sql, $conn );
if(! $result )
{
  die('Could not get data: ' . mysql_error());
}
?>

2 个答案:

答案 0 :(得分:0)

我在mySQL Workbench中试过这个并且工作正常

   <?php
    $servername = "localhost";
    $username = "root";
    $password = "";



    $year = $_POST['year'];
    $permit = $_POST['permit'];
    $range_to = $_POST['range_to'];
    $range_from = $_POST['range_from'];
    $permitArray = implode('","', (array)$permit);
    // Create connection
    $conn = mysql_connect($servername, $username, $password);
    // Check connection
    if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT  permit_type, count(id) as 'no',DATE_FORMAT(date,'%M %Y') as 'month', sum(total_amount) as 'amount'     
        FROM payments
        WHERE permit_type IN ('$permitArray') AND date like '%$year%'
        group by permit_type, DATE_FORMAT(date,'%M %Y')
        ";
        var_dump($permitArray,$year);

mysql_select_db('cpdo_db');
$result = mysql_query( $sql, $conn );
if(! $result )
{
  die('Could not get data: ' . mysql_error());
}
?>

    <div id="tabs">
            <div id="tabs-1" class="tab-pad">
                <table class="table table-striped table-bordered" border="1" id="locdata" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                        <center><h1><?php echo $year; ?></h1></center>
                            <th>Month</th>
                            <th>No</th>
                            <th>Permit Type</th>
                            <th>Total Amount</th>

                        </tr>
                    </thead>
                    <tbody>
                                                                                                                <?php 


                            while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
                                //Creates a loop to loop through results
                                echo '<tr>';
                                    echo '<td>'.'<input type="text" name="permit-type" style="border:none;" readonly value="'.$row['month'].'"/>'.'</td>';
                                        echo '<td>'.'<input name="total-no" type="text" style="border:none;" readonly value="'.$row['no'].'"/>'.'</td>';
                                        echo '<td>'.'<input name="total-no" type="text" style="border:none;" readonly value="'.$row['permit_type'].'"/>'.'</td>';
                                        echo '<td>'.'<input name="total-amount" type="text" style="border:none;" readonly value="'."P".$row['amount'].".00".'"/>'.'</td>';

                                echo '</tr>';

                        }   
                    ?>               
                    </tbody>
                </table>  
          </div>    
         </div>


<?php
mysql_close($conn);
?>

但我放入我的PHP代码,如果我选中1复选框它会返回一个结果,但是当我选中2个或更多复选框时没有结果返回

答案 1 :(得分:-1)

缺少部分代码,您将输入值传递给$ _POST变量(您可以通过ajax序列化或使用方法等形式将其传递给另一个php),但是...如果我想象你有$ permit现在作为一个输入值数组,比你应该将你的$ permits数组转换成一些逗号分隔的vlaues,可以在查询中使用:

$comma_separated_permits = implode(",", $permits);

现在您可以在查询中使用它:

... WHERE .... AND permit_type IN ($comma_separated_permits) ...

我刚刚在db表上测试了一个名为'test'的小例子,其中包含一些行

$array = array ("PALC","DP","AD");
$tosql = implode("','", $array);
$sql = ("SELECT * FROM `test` WHERE `key` IN ('$tosql')");
$result = $db -> query($sql) -> fetchAll(PDO::FETCH_COLUMN,0);
var_dump ($result); 

它工作得很好......在IN()clausule中使用字符串而不是整数时只需要注意。我的答案不包含对数组表单复选框进行建模,因为您询问如何查询db

使用html部分的完整示例:

<form action="#" method="post">
  <input type="checkbox" name="checks[]" value="PALC" />PALC
  <input type="checkbox" name="checks[]" value="DP" />DP
  <input type="checkbox" name="checks[]" value="AD" />AD
  <input type="submit" />
</form>

然后添加php:

if (!empty($_POST['checks'])){
    $array = $_POST['checks'];
    $tosql = implode("','", $array);
    $sql = ("SELECT * FROM `test` WHERE `key` IN ('$tosql')");
    $result = $db -> query($sql) -> fetchAll(PDO::FETCH_COLUMN,0);
var_dump ($result);
}