在CSV下载时插入表记录(日志条目)

时间:2017-02-10 01:42:41

标签: php mysqli

我对PHP很新,这是我的第一个stackoverflow问题:

我希望用户能够轻松了解自上次从我正在构建的应用程序中下载CSV文件以来数据何时发生了变化。 CSV导出有效,我可以确定是否有任何行的时间戳位于导出日志表中存储的时间戳之后。我的问题是我无法弄清楚如何在日志表中存储时间戳。当用户单击链接以导出文件时,执行以下代码以生成文件。最后四行是我尝试进行日志输入。 $ con是有效且有效且我的方法在常规PHP页面中工作,但我无法弄清楚如何使其在CSV下载页面中工作,我不知道有任何其他方式来进行用户输入点击下载。我愿意接受最佳方法。

我真的很感谢你的时间 - 提前感谢!

<?php
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=speakers.csv");
header("Pragma: no-cache");
header("Expires: 0");

require_once("../scripts/dbcon.inc.php");
require_once("../scripts/presentersessions.inc.php");

$query = "SELECT DISTINCT sp.person_id, CONCAT(p.person_display_name, ' ', p.person_last_name) person_name, '' session_titles, p.person_bio FROM person p INNER JOIN presenters sp ON p.person_id = sp.person_id ORDER BY p.person_last_name, p.person_display_name";
$result = mysqli_query($con, $query) or die('failed query:  '.$query);
$num_rows = mysqli_num_rows($result);
$output = fopen("php://output", "w");

fputcsv($output, array("Item ID (Optional)","Name","Sub-Title (i.e. Location, Table/Booth, or Title/Sponsorship Level)","Description (Optional)","Location/Room"));

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    #echo '"","'.htmlspecialchars($row['person_display_name']).' '.htmlspecialchars($row['person_last_name']).'","'.htmlspecialchars($row['session_title']).'","'.htmlspecialchars($row['person_bio']).'"\n"';
    $person_id = $row['person_id'];
    $row['person_name'] = $row['person_name'];
    $row['session_titles'] = presenter_sessions($conference_id, $person_id);
    $row['person_id'] = '';
    fputcsv($output, $row);
}

date_default_timezone_set('US/Eastern');
$export_timestamp = date("Y-m-d H:i:s");
$query_insert_log_entry = "INSERT INTO export_log (export_type, export_timestamp, person_id) VALUES ('$export_type', '$export_timestamp', {$_SESSION['valid_accbo_person_id']})";
$result_insert_log_entry = mysqli_query($con, $query_insert_log_entry);

1 个答案:

答案 0 :(得分:0)

确保您设置此可变$ export_type,并且可以在此页面上访问$ _SESSION [&#39; valid_accbo_person_id&#39;]。

我认为问题出在这一行 -

$query_insert_log_entry = "INSERT INTO export_log (export_type, export_timestamp, person_id) VALUES ('$export_type', '$export_timestamp', {$_SESSION['valid_accbo_person_id']})";

从以下变量中删除那些单引号 -

$query_insert_log_entry = "INSERT INTO export_log (export_type, export_timestamp, person_id) VALUES ('{$export_type}', '{$export_timestamp}', '{$_SESSION['valid_accbo_person_id']}')";