0

我有一个数据库 CSV 导出工作正常,但我希望用户能够为导出过程选择一个日期范围。

这是我用于日期选择的 HTML 部分(日期格式为mm/dd/yyyy):

<form action="csv_export_arp.php">
 <p>Select a date range from </p><label style="color:#FFF;" for="from">From</label>
 <input type="text" id="from" name="from" value="from" />
 <label style="color:#FFF;" for="to" >to</label>
 <input type="text" id="to" name="to" value="to"/>
 <input name="export" type="submit" value="Export Pending ARP CSV" />
</form>

我的 CSV 导出与日期范围不同=

$sql_query = "SELECT ticket_number AS 'Ticket number', first_name AS 'First name', surname AS
'Last name', email AS 'Email address', product AS 'Product', retailer AS 'Retailer', dop AS 
'Date of purchase', message AS 'Message', address AS 'Postal address', DATE_FORMAT(created, 
 '%m/%d/%Y') AS 'Date created', status AS 'Status', action AS 'Action', comment AS 'Comment', 
 resolution AS 'Resolution' FROM $table WHERE status='Pending ARP'
 AND created >= DATE_FORMAT('from') AND created <  DATE_FORMAT('to')" ;

我无法将我从表单(往返)获得的值与WHERE日期查询(状态正常)链接起来,如何做到这一点?非常感谢任何帮助

 AND created >= DATE_FORMAT('from') AND created <  DATE_FORMAT('to')" ;

我觉得这是重要的一行,如何将此查询与表单上的日期范围选择联系起来?

编辑:

这是脚本的第一部分:

<?php

$host = 'xxxxx'; // MYSQL database host adress
$db = 'wxxxxx'; // MYSQL database name
$user = 'xxxxx'; // Mysql Datbase user
$pass = 'xxxxxx'; // Mysql Datbase password
$from_data = $_POST['from']; //Should be sanitized before use in the query, or use PDO.
$to_data   = $_POST['to']; //Should be sanitized before use in the query, or use PDO. 
// Connect to the database
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db);

require 'scripts/exportcsv_arp.inc.php';

$table="support_users"; // this is the tablename that you want to export to csv from mysql.

exportMysqlToCsv($table);

?>

这是脚本的第二部分 (exportcsv_arp.inc.php)

<?php
function exportMysqlToCsv($table,$filename = 'export_arp.csv')
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "SELECT ticket_number AS 'Ticket number', first_name AS 'First name', surname AS 'Last name', email AS 'Email address', product AS 'Product', retailer AS 'Retailer', dop AS 'Date of purchase', message AS 'Message', address AS 'Postal address', DATE_FORMAT(created, '%m/%d/%Y') AS 'Date created', status AS 'Status', action AS 'Action', comment AS 'Comment', resolution AS 'Resolution' FROM $table WHERE status='Pending ARP' created >= DATE_FORMAT('" . $from_data . "', '%d/%m/%Y') AND created <  DATE_FORMAT('" . $to_data . "', '%d/%m/%Y')" ;
// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++)
{
    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
        stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = mysql_fetch_array($result))
{
    $schema_insert = '';
    for ($j = 0; $j < $fields_cnt; $j++)
    {
        if ($row[$j] == '0' || $row[$j] != '')
        {
            if ($csv_enclosed == '')
            {
                $schema_insert .= $row[$j];
            } else
            {
                $schema_insert .= $csv_enclosed .
                str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
            }
        } else
        {
            $schema_insert .= '';
        }
        if ($j < $fields_cnt - 1)
        {
            $schema_insert .= $csv_separator;
        }
    } // end for
    $out .= $schema_insert;
    $out .= $csv_terminated;
} // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
//header("Content-type: text/x-csv");
//header("Content-type: text/csv");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
 }
 ?>
4

1 回答 1

1
$from_data = $_POST['from']; //Should be sanitized before use in the query, or use PDO.
$to_data   = $_POST['to']; //Should be sanitized before use in the query, or use PDO.

您提取的查询部分应该是

AND created >= DATE_FORMAT('" . $from_data . "', '%d/%m/%Y') AND created <  DATE_FORMAT('" . $to_data . "', '%d/%m/%Y')
于 2013-05-02T15:04:31.557 回答