Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql中打印表后导出到csv_Php_Mysql_Forms_Csv - Fatal编程技术网

Php 在mysql中打印表后导出到csv

Php 在mysql中打印表后导出到csv,php,mysql,forms,csv,Php,Mysql,Forms,Csv,我花了好几个小时想弄明白,但没有结果。用户可以在表单中选择一些过滤器,从而生成一个唯一的表,并打印到屏幕上 然后我想要一个按钮,将这个特定的表导出到一个csv文件,存储在用户计算机上 if(isset($_POST["askReport"])){ //all the variables //... $myQuery = ""SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaa

我花了好几个小时想弄明白,但没有结果。用户可以在表单中选择一些过滤器,从而生成一个唯一的表,并打印到屏幕上

然后我想要一个按钮,将这个特定的表导出到一个csv文件,存储在用户计算机上

if(isset($_POST["askReport"])){ 
//all the variables
//...
$myQuery = ""SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD"";

$result = $mysqli->query($myQuery);
  if($result->num_rows >0){
    print '<table><tr><th>.....'; //and all the headers
    print '<td>'.$row["user"].'</td>';
    ..... //and all the other columns and end of row
  }
if(isset($_POST[“askReport”]){
//所有变量
//...
$myQuery=”“选择*,时间到秒(stopwerkdagU)-时间到秒(startwerkdagU)-pauzetijdU作为totaalurenU,时间到秒(stopwerkdagU)-时间到秒(startwerkdag)-pauzetijd作为totaaluren从'newRegister`$u$aP$pD$uF$sD';
$result=$mysqli->query($myQuery);
如果($result->num_rows>0){
打印“……”;//和所有标题
打印“”。$行[“用户”]。';
..…//以及所有其他列和行的末尾
}
到目前为止,很好,我得到了一个很好的表,其中包含了我想要的结果。但接下来是棘手的部分-我如何获得下载此表的按钮?以下是我尝试的:(此代码仍然在isset($_POST[“askReport”])if语句中)

//下载选项卡
打印“下载”;
如果(isset($_POST[“下载”])){
$select=“select*,时间到秒(stopwerkdagU)-时间到秒(startwerkdagU)-pauzetijdU AS totaalurenU,时间到秒(stopwerkdagU)-时间到秒(startwerkdag)-pauzetijd AS totaaluren FROM`newRegister`$u$aP$pD$uF$sD”;
$export=mysql\u query($select)或die(“Sql错误:”.mysql\u错误());
$fields=mysql\u num\u字段($export);
对于($i=0;$i<$fields;$i++)
{
$header.=mysql\u字段\u名称($export,$i)。“\t”;
}
while($row=mysql\u fetch\u row($export))
{
$line='';
foreach(行作为$value)
{                                            
如果((!isset($value))| |($value==“”)
{
$value=“\t”;
}
其他的
{
$value=str_replace(“,“,”,$value);
$value=“.”.$value.“.”“\t”;
}
$line.=$value;
}
$data.=修剪($line)。“\n”;
}
$data=str_replace(“\r”,”,$data);
如果($data==“”)
{
$data=“\n(0)找到记录!\n”;
}
标题(“内容类型:应用程序/八位字节流”);
标题(“内容配置:附件;文件名=download.xls”);
标题(“杂注:无缓存”);
标题(“到期日:0”);
打印“$header\n$data”;
};
}   
这不会产生任何错误。它只是重新加载页面。因此我不知道它为什么不工作。感谢您的帮助。

部分解决方案

下面的代码(您的代码,只是出于测试目的注释掉了MySQL)运行良好

下载成功的原因是当我把
标签放进去的时候。试试这个代码,让我知道它是否有效

<?php
// DOWNLOAD TABEL
    print '<form method="POST"><button name=download>download</button></form>';

    if(isset($_POST["download"])){
        $select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";

/*        $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

        $fields = mysql_num_fields ( $export );

        for ( $i = 0; $i < $fields; $i++ )
        {
            $header .= mysql_field_name( $export , $i ) . "\t";
        }

        while( $row = mysql_fetch_row( $export ) )
        {
            $line = '';
            foreach( $row as $value )
            {   
                if ( ( !isset( $value ) ) || ( $value == "" ) )
                {
                    $value = "\t";
                }
                else
                {
                    $value = str_replace( '"' , '""' , $value );
                    $value = '"' . $value . '"' . "\t";
                }
                $line .= $value;
            }
            $data .= trim( $line ) . "\n";
        }
        $data = str_replace( "\r" , "" , $data );
*/

        if ( $data == "" )
        {
            $data = "\n(0) Records Found!\n";
        }

$header="abc,123";

        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=download  .xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo "$header\n$data";
    };

?>

您在哪个浏览器上进行测试?对我来说一切都很好。在Safari 8.0上测试停止使用您的自制CSV代码,并使用PHP的内置fputcsv()功能为什么您要生成csv数据发送到浏览器,同时还要将包含html标记的打印输出发送到浏览器?我正在Safari 8.0.1上测试您使用的浏览器?使用Safari 8.0.1。谢谢。
<?php
// DOWNLOAD TABEL
    print '<form method="POST"><button name=download>download</button></form>';

    if(isset($_POST["download"])){
        $select = "SELECT *, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdagU) - pauzetijdU AS totaalurenU, TIME_TO_SEC(stopwerkdagU) - TIME_TO_SEC(startwerkdag) - pauzetijd AS totaaluren FROM `newRegister` $u $aP $pD $uF $sD";

/*        $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

        $fields = mysql_num_fields ( $export );

        for ( $i = 0; $i < $fields; $i++ )
        {
            $header .= mysql_field_name( $export , $i ) . "\t";
        }

        while( $row = mysql_fetch_row( $export ) )
        {
            $line = '';
            foreach( $row as $value )
            {   
                if ( ( !isset( $value ) ) || ( $value == "" ) )
                {
                    $value = "\t";
                }
                else
                {
                    $value = str_replace( '"' , '""' , $value );
                    $value = '"' . $value . '"' . "\t";
                }
                $line .= $value;
            }
            $data .= trim( $line ) . "\n";
        }
        $data = str_replace( "\r" , "" , $data );
*/

        if ( $data == "" )
        {
            $data = "\n(0) Records Found!\n";
        }

$header="abc,123";

        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=download  .xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        echo "$header\n$data";
    };

?>