0

我在 FPDF 显示我想要显示的日期范围时遇到问题。它每次都会从数据库的开头提取每个日期。不管我把它放在什么范围内,它都是从开始到结束的日期。任何帮助都会有所帮助。

这是代码:

      <?php
      define('FPDF_FONTPATH','font/');
       require('mysql_table.php');

        class PDF extends PDF_MySQL_Table
       {
      function Header()
       {
       //Title
       $this->Image('eagle.png',10,6,30);
        $this->Image('eagle.png',170,6,30);
       $this->SetFont('Arial','',18);
       $this->Cell(0,6,'Range Report',0,1,'C');
       $this->Ln(20);
        //Ensure table header is output
        parent::Header();
        }
        } 

        //Connect to database
        mysql_connect('localhost','root','');
        mysql_select_db('charitabledb');

        $pdf=new PDF();
        $pdf->Open();
        $pdf->AddPage();
         //table: specify 4 columns
        $pdf->AddCol('date',20,'Date','C');
        $pdf->AddCol('transactiontype',30,'Transaction','L');
        $pdf->AddCol('account',40,'Account','L');
         $pdf->AddCol('description',60,'Description','L');
         $pdf->AddCol('Expense',20,'Expense','R');
         $pdf->AddCol('Income',20,'Income','R');
        $prop=array('HeaderColor'=>array(255,150,100),
        'color1'=>array(210,245,255),
        'color2'=>array(255,255,210),
        'padding'=>2);
        $startdate=$_Post['startdate'];
        $enddate=$_POST['enddate'];
         $pdf->Table("SELECT * FROM accounting WHERE date between '" . $startdate . "' AND '" . 
         $enddate . "' order by date ASC",$prop);

         $pdf->AddCol('',150,'');
         $pdf->AddCol('Sum(Expense)',40,'Total Expense','R');
         $prop=array('HeaderColor'=>array(255,150,100),
         'color1'=>array(210,245,255),
         'color2'=>array(255,255,210),
         'padding'=>2);
         $pdf->Table("select Sum(Expense) from accounting WHERE date between '" . $startdate . "' 
         AND '" . $enddate . "'",$prop);
         $pdf->AddCol('',150,'');
         $pdf->AddCol('Sum(Income)',40,'Total Income','R');
         $prop=array('HeaderColor'=>array(255,150,100),
         'color1'=>array(210,245,255),
         'color2'=>array(255,255,210),
         'padding'=>2);
          $pdf->Table("select Sum(Income) from accounting WHERE date between '" . $startdate . "' 
          AND '" . $enddate . "'",$prop);
          $pdf->AddCol('',150,'');
          $pdf->AddCol('Sum(Income)-Sum(Expense)',40,'Total Revenue','R');
           $prop=array('HeaderColor'=>array(255,150,100),
         'color1'=>array(210,245,255),
         'color2'=>array(255,255,210),
         'padding'=>2);
          $pdf->Table("select Sum(Income)-Sum(Expense) from accounting WHERE date between '" . 
           $startdate . "' AND '" . $enddate . "'",$prop);

           $pdf->Output();
            ?>
4

1 回答 1

0

问题出在您的 MySQL 查询中...从您的代码中提取...

$query = "SELECT * FROM accounting WHERE date between '" . $startdate . "' AND '" . $enddate . "' order by date ASC";

$startdate和的格式是$enddate什么?您可能会考虑对它们执行显式强制转换以确保 MySQL 做正确的事情。我假设日期是一个日期时间列。如果不是,请相应调整。

WHERE date BETWEEN CAST('$startdate' as datetime) AND CAST('$enddate' as datetime)

除此之外,您可以打印该查询的输出并单独通过数据库运行它。您可以调整查询,直到获得预期的结果。如果您可以在此处添加示例查询,那也会有所帮助。

附带说明,如果此应用程序用于生产环境,请转义来自用户的任何输入。否则,您将面临 SQL 注入攻击的风险。

于 2013-04-17T20:26:44.487 回答