dongzhu3548 2016-02-10 06:09
浏览 65
已采纳

PHP:插入句点并导出.csv

My code export a .csv file, depending on sql code. I inserted 3 variables start_month, finish_month and year to export the data to a specific time. I do not know how to do that by pressing the button csv export to export .csv file depending on the entered values in the three variables.

<?php
// call export function
exportMysqlToCsv('export.csv');

// export csv
function exportMysqlToCsv($filename = 'export.csv')
{
$conn = dbConnection();
// Check connection
if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
}
//var for period
$start_month = trim($_POST['start_month']);
$finish_month = trim($_POST['finish_month']);
$year = trim($_POST['year']);

$sql_query = "SELECT
      activitati.tip,
      clienti.NUME_J,
      clienti.NUME_F,
      activitati.month,
      activitati.year


FROM activitati

JOIN clienti ON clienti.ID = activitati.ID_CLIENT
JOIN dotari ON activitati.ID_DOTARE = dotari.ID


WHERE liamed.activitati.TIP = "Delivery"
      AND activitati.month BETWEEN "{$start_month}" AND "{$finish_month}"
      AND activitati.year_DATA_ACTIVITATE = "{$year}"
      LIMIT 50;";
     //execute this sql and fetch your results as needed

// Gets the data from the database
$result = $conn->query($sql_query);
$f = fopen('php://temp', 'wt');
$first = true;
while ($row = $result->fetch_assoc()) {
         if ($first) {
                 fputcsv($f, array_keys($row), ';');
                 $first = false;
         }
         fputcsv($f, $row, ';');
} // end while

$conn->close();
$size = ftell($f);
rewind($f);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: $size");
// 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");
fpassthru($f);
exit;
}
// db connection function
function dbConnection(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "delivery";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
return $conn;
}

?>
<form>
Insert start month:<input type="text" name="start_month" ><br>
Insert finish month: <input type="text" name="finish_month" ><br>
Insert year:<input type="text" name="year"><br>
<input type="submit" name="button" value="export csv">
</form>

Thank you!

</div>
  • 写回答

1条回答 默认 最新

  • duannaikuang1301 2016-02-10 06:34
    关注

    You need to change your WHERE clause to use the month and year database columns:

    This:

    AND activitati.LUNA_DATA_ACTIVITATE BETWEEN "{$start_month}" AND "{$finish_month}"
    AND activitati.ANUL_DATA_ACTIVITATE = "{$year}"
    

    becomes:

    AND activitati.month BETWEEN "{$start_month}" AND "{$finish_month}"
    AND activitati.year = "{$year}"
    

    Don't forget to sanitize your form input, you don't want a SQL vulnerability!

    Hint, hint...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么