I have looked at a few examples on using arrays in a query, but I just cant seem to get something that will help me…
I need to use an array of selected items from check boxes to the processing page and use them in a query. Note all my data is live, the data from the checkboxes to the data being used /or required from the query.
The first part, query1 calculates the totals of the selected array (from the checkboxes) and the second part, query2 displays all the data from the database from the selected array brought forward.
Can anybody help me with this query? (if only one item is selected, both queries work fine but not with multiple selections)
The name of my data array is “interest”, I have tried using implode and the IN clause in my WHERE query:
<?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 </h7><b>";
print $d_q;
echo "</b><h7> 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>";
?>
I found the solution, maybe this can help someone else who is struggling with something simular.
I first assigned the array to a variable…
Then replaced…
With this
The problem I was having with my script was that my variable is not a number, but a string therefor I had to insert “” into the implode statement. Here’s the code and it works perfectly now: