Download Link query mysql

Dear All,

I have some code to export mysql query to excel file, but iam stuck with no result.
I expect when iam done browse the report (rpt), and displaying the table in browser i can download it.
Someone can help me pls.
Here with my codes:

Display data from Mysql


<?php
	if (isset($_REQUEST['submit'])){
		if (count($_POST['rpt']) > 0){
			$date = $_POST['datepicker'];
			$rpt = implode(',',$_POST['rpt']);
			$str = ' rsk_type IN ('.$rpt.')';
		}
	$sql = "SELECT * FROM kr_rpt_daily where date_rpt = '$date' AND $str";
	$res = runsqltext($sql);
	}	
?>
<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
		$('#dataque').dataTable();

	})
</script>
<form name="frque" id="frque" method="POST" action="">
    <table width="713" border="1">
          <tr>
            <td width="211"><input type="checkbox"  name = "rpt[]" value="'BA'" />Banker Acceptance (BA) </td>
            <td width="131"><input type="checkbox"  name = "rpt[]" value="'Bond Issuer'" />Bond Issuer </td>
            <td width="202"><input type="checkbox"  name = "rpt[]" value="'Bond Presettlement'" />Bond Presettlement </td>
          </tr>
          <tr>
            <td><input type="checkbox" name = "rpt[]" value="'FX Bank'" />FX Bank</td>
            <td><input type="checkbox" name = "rpt[]" value="'FX NonBank'" />FX NonBank</td>
            <td><input type="checkbox" name = "rpt[]" value="'FX Option Bank'" />FX Option Bank</td>
          </tr>
          <tr>
            <td><input type="checkbox" name = "rpt[]" value="'IRS Bank'" />IRS Bank</td>
            <td><input type="checkbox" name = "rpt[]" value="'IRS NonBank'" />IRS NonBank</td>
            <td><input type="checkbox" name = "rpt[]" value="'MM Bank'" />MM Bank</td>
          </tr>
          <tr>
            <td><input type="checkbox" name = "rpt[]" value="'MM NonBank'" />MM NonBank</td>
            <td><input type="checkbox" name = "rpt[]" value="'FX Settlement'" />FX Settlement</td>
          </tr>
    </table>
    <p>Select Date From: <input type="text" id="datepicker" name="datepicker"/><input name ="submit" type="submit" value="Search" />
</form>	

<div id="que_res">
	<p>Result</p> <a href="contents/download1.php">Download To Excel</a><br/><br/>
    <div id="dataq">
	<table id="dataque">
    	<thead>
        	<tr>
            	<th>Report Date</th>
                <th>Counterparty Label</th>
                <th>Counterparty Name</th>
                <th>Risk Type</th>
                <th>Maximum Tenor</th>
                <th>Limit</th>
                <th>CCY</th>
                <th>Utilization</th>
                <th>Limit Available</th>
                <th>Limit Expiry</th>
                <th>Limit Status</th>
                <th>Expiry Status</th>
                <th>Market Value</th>
                <th>Percentage Of Limit Used</th>
            </tr>
        </thead>
        <tbody>
        	<?php
				while($row = mysql_fetch_array($res)){
					?>
                    <tr>
                    	<td><?=$row['date_rpt']?></td>
                        <td><?=$row['label_ctp']?></td>
                        <td><?=$row['name_ctp']?></td>
                        <td><?=$row['rsk_type']?></td>
                        <td><?=$row['max_ten']?></td>
                        <td><?=$row['lmt']?></td>
                        <td><?=$row['ccy']?></td>
                        <td><?=$row['util']?></td>
                        <td><?=$row['avai']?></td>
                        <td><?=$row['exp']?></td>
                        <td><?=$row['lmt_stat']?></td>
                        <td><?=$row['exp_stat']?></td>
                        <td><?=$row['mtm_val']?></td>
                        <td><?=$row['perc']?></td>
                    </tr>
            		<?php
				}
				?>
        </tbody>
    </table>
    </div>
</div>


Download to Excel:


<?php
/*
* Export Mysql Data in excel or CSV format using PHP
* Downloaded from http://DevZone.co.in
*/

// Connect to database server and select database
$con = mysql_connect('localhost','root','');
mysql_select_db('risk',$con);


	$date = $_POST['datepicker'];
	$rpt = implode(',',$_POST['rpt']);
	$str = ' rsk_type IN ('.$rpt.')';

// retrive data which you want to export
$query = "SELECT * FROM kr_rpt_daily where date_rpt = '$date' AND $str";
$header = '';
$data ='';
$export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );

// extract the field names for header
$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\	";
}

// export data
while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\	";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\	";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\
";
}
$data = str_replace( "\\r" , "" , $data );

if ( $data == "" )
{
    $data = "\
No Record(s) Found!\
";
}

// allow exported file to download forcefully
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\
$data";

?>

I don’t see how you’re passing the date or rpt array from the display page to the download page - the download php code is looking in $_POST, but the page is called by a standard href link. *


<p>Result</p> <a href="contents/download1.php">Download To Excel</a><br/><br/>

Also note you should look at migrating either to mysqli or pdo for database access as the mysql functions you use are being deprecated.

    • note that I am learning, so this might not be the issue.