0

What I'm basically trying to do is retrieve an entire row from mysql database on checkbox selection. This code works fine for single selection. But on multiple selection it still retrieves a single row. How do I loop through it?

$sql = "Select * from $DB_TBLName WHERE Delivery_no = '{$_REQUEST['check'][0]}'" ;
echo $sql;
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
if(isset($_POST['submit'])){
   if(!empty($_POST['check'])) {
      // Loop to store and display values of individual checked checkbox.
      foreach($_POST['check'] as $selected) {
         echo $selected ;
      }
   }
}        
$sep = "\t";     
for ($i = 0; $i < mysql_num_fields($result); $i++) {
   echo mysql_field_name($result,$i) . "\t";
}
print("\n");      
while($row = mysql_fetch_row($result)){
   $schema_insert = "";
   for($j=0; $j<mysql_num_fields($result);$j++) {
      if(!isset($row[$j]))
         $schema_insert .= "NULL".$sep;
      elseif ($row[$j] != "")
         $schema_insert .= "$row[$j]".$sep;
      else
         $schema_insert .= "".$sep;
   }
   $schema_insert = str_replace($sep."$", "", $schema_insert);
   $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
   $schema_insert .= "\t";
   print(trim($schema_insert));
   print "\n";
}  
$file_ending = "xls";
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

HTML CODE

<td class="border-right" align="center"><input type="checkbox" name="check[]" id="check[]" value= "<?php echo $row['Delivery_no']; ?>" /></td>    
<td><?php echo $row['Delivery_no'];?></td>  
<td><?php echo $row['Invoice_no'];?></td>
<td><?php echo $row['Bill_date'];?></td>
<td><?php echo $row['Bill_to_party'];?></td>
<td><?php echo $row['Quantity'];?></td>
<td><?php echo $row['Brand']; } ?></td>
</tr>   
</table>
<br>
<input type= "submit" name="submit" id="submit" value="submit" class="btn btn-primary button-loading">

1 Answer 1

2

You appear to build up the SQL at the start but only ever use the first element of the checks input array.

You need to use all the elements

Simply knocked together, something like this:-

if (is_array($_REQUEST['check']) and count($_REQUEST['check']) > 0)
{
    $checks = implode(',', array_map('intval', $_REQUEST['check']));

    $sql = "Select * from $DB_TBLName WHERE Delivery_no IN ($checks)" ;
    echo $sql;
    $result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
    if(isset($_POST['submit'])){
       if(!empty($_POST['check'])) {
          // Loop to store and display values of individual checked checkbox.
          foreach($_POST['check'] as $selected) {
             echo $selected ;
          }
       }
    }        
    $sep = "\t";     
    for ($i = 0; $i < mysql_num_fields($result); $i++) {
       echo mysql_field_name($result,$i) . "\t";
    }
    print("\n");      
    while($row = mysql_fetch_row($result)){
       $schema_insert = "";
       for($j=0; $j<mysql_num_fields($result);$j++) {
          if(!isset($row[$j]))
             $schema_insert .= "NULL".$sep;
          elseif ($row[$j] != "")
             $schema_insert .= "$row[$j]".$sep;
          else
             $schema_insert .= "".$sep;
       }
       $schema_insert = str_replace($sep."$", "", $schema_insert);
       $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
       $schema_insert .= "\t";
       print(trim($schema_insert));
       print "\n";
    }  
    $file_ending = "xls";
    header("Content-Type: application/xls");    
    header("Content-Disposition: attachment; filename=$filename.xls");  
    header("Pragma: no-cache"); 
    header("Expires: 0");
}

Note - you are using the old obsolete mysql_* functions rather than the current mysqli_* functions (or the PDO equivalents). I would advise you to swap over. This would also allow you to use parameterised queries which are safer. Your current query is wide open to SQL injection attacks.

Sign up to request clarification or add additional context in comments.

6 Comments

i think the $selected in foreach() has to be used in the query.. how do we do it?
By the time you have looped around to populate $selected you have already tried to use the values in the SQL. Hence why I have just used implode to get the values and use them in an IN clause in the SQL.
okay.. so the problem is that nothing is getting displayed in the excel file except for headers :/
i $echod the query.. this is what it looks like on excel for 2 selected checkbox..... Select * from billing WHERE Delivery_no IN (2147483647,2147483647)Select * from billing WHERE Delivery_no IN (2147483647,2147483647)Select * from billing WHERE Delivery_no IN (2147483647,2147483647)Select * from billing WHERE Delivery_no IN (2147483647,2147483647)... I have no idea what those values in the bracket are.. it is not in the database
Can you post the source of the form that you would submit (ie, with the fields filled in)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.