Checkbox id in mysql query in php


<?php
error_reporting(0);
require("connect.php");


$details = "SELECT qty_size, SUM(qty_qty) AS sum FROM tbl_qty WHERE qty_tbl_order_id IN (133, 137) GROUP BY qty_size";
$details_result = mysql_query( $details)
    or die ( "Couldn't get Products: ".mysql_error() );
while ( $det_row = mysql_fetch_array ( $details_result ) )
{
echo "<b>&nbsp ".$det_row[ 'qty_size' ].":&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp </b>".$det_row[ 'sum' ]."<br>";
}

?>

Hi

I am getting below out put with the above script. Its ok.

128: 304
140: 429
152: 429
164: 358
176: 268


But when i use below script,


<?php
error_reporting(0);
require("connect.php");

// get a total ...
if(!empty($_POST['items8'])) {
    foreach($_POST['items8'] as $item) {


$details = "SELECT qty_size, SUM(qty_qty) AS sum FROM tbl_qty WHERE qty_tbl_order_id='$item' GROUP BY qty_size";
$details_result = mysql_query( $details)
    or die ( "Couldn't get Products: ".mysql_error() );
while ( $det_row = mysql_fetch_array ( $details_result ) )
{
echo "<b>&nbsp ".$det_row[ 'qty_size' ].":&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp </b>".$det_row[ 'sum' ]."<br>";
}
}
}

?>

then i get the out put as below.

128: 183
140: 258
152: 258
164: 215
176: 161
128: 121
140: 171
152: 171
164: 143
176: 107

But my requirement is as
128: 304
140: 429
152: 429
164: 358
176: 268

Please advise me where is the problem.

Thanks

Echo out $details in your second script, and see what the query looks like.

If the contents of :


$_POST['items8']

do not equal 133 and 137 then you are effectively doing a different query so inspect it with


 var_dump($_POST['items8'])

Using IN is probably a faster way of doing things, so you should consider replicating the first query and using this:


$in_clause = "(" . join(","$_POST['items8']) . ")";

$details = "SELECT qty_size, SUM(qty_qty) AS sum FROM tbl_qty WHERE qty_tbl_order_id IN $in_clause ORDER BY qty_size";