在查询的WHERE子句中使用checkbox数组来查找所有选定的数据

时间:2012-09-19 08:17:19

标签: php mysql

我已经看过一些关于在查询中使用数组的例子,但我似乎无法得到一些能帮助我的东西......

我需要使用复选框中的所选项目数组到处理页面,并在查询中使用它们。请注意我的所有数据都是实时的,从复选框到查询中使用/或要求的数据的数据。

第一部分,query1计算所选数组的总数(来自复选框),第二部分,query2显示来自所选数组的数据库中的所有数据。

有人可以帮我解决这个问题吗? (如果只选择了一个项目,则两个查询都可以正常工作,但不能选择多个选项) 我的数据数组的名称是“兴趣”,我尝试在我的WHERE查询中使用implode和IN子句:

<?php
$date = date("F-Y",$_SESSION[diesel_date]);
$matches = implode(',', $_POST[interest]);

include("../xxx.xxx");
$cxn = mysqli_connect($host,$user,$password,$dbname);
$query = "SELECT SUM(`diesel_qty`) d_q FROM `diesel` 
            WHERE MONTH(`diesel_date`)= MONTH( '$dd' )
            AND `diesel_vehicle_no` IN ('$matches') ";
$result = mysqli_query($cxn,$query);
    while($row = mysqli_fetch_assoc($result))
    {
    extract($row);
    echo "<h7>Total Diesel Filled during $dd is&nbsp;</h7><b>";
    print $d_q;
    echo "</b><h7>&nbsp;Litres.</h7><br><br>\n";
    }
?>
<?php
      include("../xxx.xxx");
$cxn = mysqli_connect($host,$user,$password,$dbname)
    or die ("Couldn't connect to server.");
$query = "SELECT
                  `diesel_date`, 
                  `diesel_time`, 
                  `location_id`, 
                  `company_id`, 
                  `diesel_vehicle_no`, 
                  `diesel_driver_name`, 
                  `diesel_qty`,
                  `diesel_feed_id` 
    FROM`diesel` 
    WHERE MONTH(`diesel_date`)= MONTH( '$dd' )
    AND `diesel_vehicle_no` IN ('$matches') 
    ORDER BY `diesel_date`";
$result = mysqli_query($cxn,$query)
    or die ("Couldn't execute query.");

echo "<table><br>
<tr>
 <th>DieselFillDate</th>
 <th>Time</th>
 <th>Location</th>
 <th>CompanyOwning Plant/Vehicle</th>
 <th>Plant/Vehicle No</th>
 <th>DriversName</th>
 <th>Quantity</th>
 <th>DieselFeed</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
    extract($row);
    echo "<tr>\n
        <td width='100'>$diesel_date</td>\n
        <td>$diesel_time</td>\n
        <td>$location_id</td>\n
        <td>$company_id</td>\n
        <td>$diesel_vehicle_no</td>\n
        <td>$diesel_driver_name</td>\n
        <td>$diesel_qty</td>\n
        <td>$diesel_feed_id</td>\n
        </tr>\n";
}
echo "</table><br>";
?>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,也许这可以帮助那些正在努力解决某些问题的人。

我首先将数组分配给变量......

$matches = $_POST[interest];

然后更换......

$matches = implode(',', $_POST[interest]);

有了这个

$amf = implode("', '", array_keys($matches));

我的脚本遇到的问题是我的变量不是数字,而是一个字符串,我必须在implode语句中插入“”。这是代码,它现在完美运行:

<?php 
$date = date("F-Y",$_SESSION[diesel_date]); 
$amf = implode("', '", array_keys($matches));

include("../xxx.xxx"); 
$cxn = mysqli_connect($host,$user,$password,$dbname); 
$query = "SELECT SUM(`diesel_qty`) d_q FROM `diesel`  
        WHERE MONTH(`diesel_date`)= MONTH( '$dd' ) 
        AND `diesel_vehicle_no` IN ('$matches') "; 
$result = mysqli_query($cxn,$query); 
    while($row = mysqli_fetch_assoc($result)) 
    { 
    extract($row); 
    echo "<h7>Total Diesel Filled during $dd is&nbsp;</h7><b>"; 
    print $d_q; 
    echo "</b><h7>&nbsp;Litres.</h7><br><br>\n"; 
    } 
?> 
<?php 
  include("../xxx.xxx"); 
$cxn = mysqli_connect($host,$user,$password,$dbname) 
    or die ("Couldn't connect to server."); 
$query = "SELECT 
              `diesel_date`,  
              `diesel_time`,  
              `location_id`,  
              `company_id`,  
              `diesel_vehicle_no`,  
              `diesel_driver_name`,  
              `diesel_qty`, 
              `diesel_feed_id`  
    FROM`diesel`  
    WHERE MONTH(`diesel_date`)= MONTH( '$dd' ) 
    AND `diesel_vehicle_no` IN ('$matches')  
    ORDER BY `diesel_date`"; 
$result = mysqli_query($cxn,$query) 
or die ("Couldn't execute query."); 

echo "<table><br> 
<tr> 
 <th>DieselFillDate</th> 
 <th>Time</th> 
 <th>Location</th> 
 <th>CompanyOwning Plant/Vehicle</th> 
 <th>Plant/Vehicle No</th> 
 <th>DriversName</th> 
 <th>Quantity</th> 
 <th>DieselFeed</th> 
</tr>"; 
while($row = mysqli_fetch_assoc($result)) 
{ 
    extract($row); 
    echo "<tr>\n 
    <td width='100'>$diesel_date</td>\n 
    <td>$diesel_time</td>\n 
    <td>$location_id</td>\n 
    <td>$company_id</td>\n 
    <td>$diesel_vehicle_no</td>\n 
    <td>$diesel_driver_name</td>\n 
    <td>$diesel_qty</td>\n 
    <td>$diesel_feed_id</td>\n 
    </tr>\n"; 
} 
echo "</table><br>"; 
?>