Click here to Skip to main content
15,920,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HTML
<body> 

<div class="container">
 <form action="export.php" method="post">
                                                                                        
<table class="table">
  <thead>
               <tr class="warning"> 
                 <th></th>	

                 <th>Delivery No.</th>
               <th>Invoice Number</th>  				 
                 <th>Bill Date</th>         
             	<th>Customer</th>             
             	 <th>Quantity</th>   
                  <th>Brand</th>				 
                      
               </tr>  
			   	   
</thead>
			  
     <?php while ($row = mysqli_fetch_array($filter_result)){ ?>
                  <tr>
				  
<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>

			<div class="control-group">
				<div class="controls">
					<button type="submit" id="submit" name="submit" value="submit data" class="btn btn-primary button-loading" data-loading-text="Loading...">Download !</button>
				</div>
			</div>
		</form>

		</div>
</body>         
</html>


export.php
PHP
$DB_Server = "localhost"; //MySQL Server    
$DB_Username = "root"; //MySQL Username     
$DB_Password = "";             //MySQL Password     
$DB_DBName = "daman_plant";         //MySQL Database Name  
$DB_TBLName = "billing"; //MySQL Table Name   
$filename = "excelfilename";         //File Name
/*******YOU DO NOT NEED TO EDIT ANYTHING BELOW THIS LINE*******/    
//create MySQL connection   
$sql = "Select * from $DB_TBLName WHERE Delivery_no = '$_REQUEST[Delivery_no]'" ;
 //$sql = "SELECT * FROM $DB_TBLName  WHERE Delivery_no = 8153064570" ;

$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database   
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());   
//execute query 
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());    
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");

/*******Start of Formatting for Excel*******/   
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");    
//end of printing column names  
//start while loop to get data
    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";
    }


What I have tried:

I want to download the contents of database selected on checkbox on button click in Excel format. I was to told to use a session to carry the value of checkbox to the next page. But I don't know how. Help me with it or any other alternative possible.
Posted
Updated 29-Mar-17 10:03am
v3
Comments
Richard Deeming 28-Mar-17 9:45am    
An unformatted code-dump is not a question.

Click "Improve question" and remove the irrelevant parts of your code. Format the relevant parts using the "code" button in the toolbar.

Then add a proper description of what you are trying to do, what you have tried, and what the problem is.
TARS166 28-Mar-17 14:42pm    
Done. Kindly look into code.
Richard Deeming 28-Mar-17 9:47am    
Also, you have already posted this code-dump yesterday:
https://www.codeproject.com/Questions/1178942/How-to-download-records-from-phpmyadmin-db-in-exce[^]

Delete one of these duplicate questions.

1 solution

Quote:
I'm trying to download records from mysql in excel on checkbox selection using session

One of the reasons you don't get answers, is that the question is poorly constructed.
First, the question is not related to Excel. The file generated is a simple .csv file with tabs, it can be used with about anything including Excel, but not only.
second, Your problem is not about read a database either

Your real question is "How to read the user input in my php code ?"

You probably also need to learn how to call php code on server side from client browser.
In this site, your will find a lot of things related to web deb:
PHP 5 Tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900