0

我已经查看了一些在查询中使用数组的示例,但我似乎无法得到对我有帮助的东西......

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

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

有人可以帮我解决这个问题吗?(如果只选择了一项,则两个查询都可以正常工作,但不能进行多项选择)我的数据数组的名称是“interest”,我尝试在 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>";
?>
4

1 回答 1

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>"; 
?> 
于 2012-09-19T13:56:48.653 回答