1

I've a problem when exporting data from mysql to pdf with ezpdf via php. The problem is when execute the file it's always said undefined variable $sesen_uk. But when i change where condition to manually like production, there's no problem.

<?php
require 'config.php';
require 'class.ezpdf.php';

$sesen_uk     = $_SESSION['unitkerja'];

$sql  = mysqli_query($conn, "SELECT * FROM arsip WHERE lokasi = '$sesen_uk' ");
$i    = 1;
if (mysqli_num_rows($sql) > 0) // Menampilkan data apabila ada datanya
{
  while($tampil = mysqli_fetch_array($sql)) 
  {
    $data[$i] = array ( 'No. Urut'      => $i,
                    'No. Surat'     => $tampil['no_surat'],
                    'Uraian'        => $tampil['uraian'],
                    'Lokasi'        => $tampil['lokasi'],
                  );
            $i++;
  }

$pdf = new Cezpdf('A4','portrait');
$pdf->ezSetCmMargins(1, 3, 3, 3);
$pdf->addJpegFromFile('images/logo.jpg',45,758,40);
$pdf->ezText('Monthly Report', 17, array('justification' => 'center'));
$pdf->ezSetDy(-10);
$pdf->ezSetDy(-10);

$pdf->line(31, 755, 565, 755);
$pdf->ezTable($data, '', '', '');
$pdf->ezSetDy(-50);

$pdf->ezStartPageNumbers(564, 20, 8);
$pdf->ezStream();
}  
  else
  {
    echo '<script>alert("No data");location.replace("report.php")</script>';
  }
?>

Any help will be great, thanks.

wakped
  • 67
  • 7

1 Answers1

2

Your code doesn't contain session_start();, therefore the session was never started.

It is required to reside inside all files using sessions.

It's always best to first check if it is set/not empty.

I.e.: if(isset($_SESSION['unitkerja'])){...} else{...}

or:

if(isset($_SESSION['unitkerja']) && !empty($_SESSION['unitkerja'])){...}

References:

Plus, you are open to an SQL injection. Use a prepared statement.

References:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141