PHP发布到第二个PHP与pgsql查询获得405错误

时间:2017-10-05 16:26:38

标签: php postgresql

我有一个PHP文档,其中用户选择随后发布到另一个PHP文档的变量,该文档使用fputcsv启动浏览器下载由pgsql查询填充的csv文件。这工作正常但它突然停止并导致405 Not Allowed错误...我明白这意味着该方法是不允许的,但我不知道为什么会突然出现这种情况。一些研究表明,我的isp可能是罪魁祸首所以我试图通过我的移动热点并得到相同的结果。我已经在多个浏览器中进行了测试,但没有一个正在运行。数据库由AWS提供。

<?php
// show error messages
ini_set('error_reporting', E_ALL);
ini_set("display_errors", 1);

$datea= $_POST["userDatea"];
$media= $_POST["userMedia"];
$datez= $_POST["userDatez"];
$media_names = "'".implode( "%','", $media)."%'";
//print_r($_POST);


//var_dump($media_names);

 if( !empty($_SERVER['REQUEST_METHOD']) && (strcasecmp($_SERVER['REQUEST_METHOD'], 'post')===0)  ) {
// Create connection
$conn = pg_connect("host= dbname= user= password=");//deleted info for security

// Check connection
if (!$conn) {
echo "Did not connect.\n";
exit;
}
$result = pg_query($conn,
"SELECT
p.store,
Count(b.starttime) as Plays

FROM
public.billing b,
public.medias m,
public.players p

WHERE
b.mediaitemid = m.id and
p.id = b.playerid and
m.name LIKE any (array[$media_names]) and
b.starttime >= date('$datea') and 
b.starttime < date('$datez')+1 and
m.startdate >  '2015-01-01' and

GROUP BY
p.store

ORDER BY
p.store;");

if (!$result) {
echo "Query failed.\n";
exit;
}

 $num_fields = pg_num_fields($result);
    $headers = array();

    for ($i = 0; $i < $num_fields; $i++) 
    {
        $headers[] = pg_field_name($result , $i);
    }

    $fp = fopen('php://output', 'w');
    if ($fp && $result)
    {
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; 
            filename="'.$_POST['filename'].'.csv"');
            header('Pragma: no-cache');
            header('Expires: 0');
            fputcsv($fp, $headers);

            while ($row = pg_fetch_row($result)) 
            {
                 fputcsv($fp, array_values($row));
            }
            die;

}
        exit('It works');
    }
?>

0 个答案:

没有答案