我的数据库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;
}
?>
答案 0 :(得分: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')