Search code examples
phphtmlmysqlfpdf

fpdf error and view table error


enter image description here

 $sqlinsert="SELECT * FROM imagetable "; 
    $result = mysqli_query($con, $sqlinsert);

    echo "<table border='1'>";
    echo "<tr>
              <th>ID</th>            
              <th>School Code</th>
              <th>Category</th>
              <th> School name </th>
              <th> School Address </th> 
              <th> School name </th>

          </tr>";

    while($row = mysqli_fetch_assoc($result)){
        $id=  $row['ID'];
        echo "<tr>
                  <td>";
                  echo $row['ID'];
                  echo "<td>";
                  echo $row['category'];
                  echo "<td>";
                  echo $row['sname'];
                  echo "<td>";
                  echo   $row['sadd'];
                  echo "<td>";
                  echo   $row['filename'];

                  echo "<td>";
                 ?>             


           <form name="form" method="POST" action="parentssubmit.php">
         <input  type="hidden" value="<?php echo  $id;?>" name="search">
         <input type="submit"  value="View" name="View">
       </form>
    <?php   } 

         echo "</td></tr>";
    echo "</table>";
    ========================================================================
    if (isset($_POST['View']) ){
     $pdf=new fpdf();
    $pdf->ADDPage();
    $pdf->setfont('Arial','B', 16);  
    $pdf->Cell(40,10, 'sname',1,0,'c');
    $pdf->Cell(40,10, 'sadd',1,0,'c');
    $pdf->Cell(40,10, 'category',1,0,'c');
    $pdf->Cell(40,10, 'scode',1,0,'c');


         $con = mysqli_connect("localhost","root","","school");
           if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }


       $file_filename=$_FILES['file']['name'];

       $target_path  =  "Newfolder1";


     $image_path = $target_path . DIRECTORY_SEPARATOR . "filename";

        $image_format = strtolower(pathinfo('filename', PATHINFO_EXTENSION));
        $sql="SELECT * FROM imagetable WHERE ID= '$_POST[search]'";
        $result = mysqli_query($con, $sql);
          if(!mysqli_query($con, $sql)){
                    die( "Could not execute sql: $sql"); 
                     }

    // build the data array from the database records.
    While($row = mysqli_fetch_array($result)) {
           $pdf->Cell(40,10, $row['sname'],1,0,'c');
            $pdf->Cell(40,10, $row['sadd'],1,0,'c');
            $pdf->Cell(40,10, $row['category'],1,0,'c');


    $pdf->Image($image_path,50,100,50,20,$image_format);

           $pdf->ln();}
    }
    $pdf->output();

this is two pages and error is coming out Undefined index:

file Undefined index: file so please try to point out the mistake because main error is showing when i added the image field then the error is started popping out.

  • I am trying to fetch the image from the database and trying to show it in PDF format.

Solution

  • First, you should close all your <tr> and <td> tags. Second, you're not sending any file with your form, and hence you're getting this Undefined index: file error, so remove these lines,

    $file_filename=$_FILES['file']['name'];
    $target_path  =  "Newfolder1";
    $image_path = $target_path . DIRECTORY_SEPARATOR . "filename";
    $image_format = strtolower(pathinfo('filename', PATHINFO_EXTENSION));
    

    So after user hits view button, you should process your form and display image like this,

    // your code
    
    if (isset($_POST['View']) ){
        $pdf=new fpdf();
        $pdf->ADDPage();
        $pdf->setfont('Arial','B', 16);  
        $pdf->Cell(40,10, 'sname',1,0,'c');
        $pdf->Cell(40,10, 'sadd',1,0,'c');
        $pdf->Cell(40,10, 'category',1,0,'c');
        $pdf->Cell(40,10, 'scode',1,0,'c');
    
    
        $con = mysqli_connect("localhost","root","","school");
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    
        $sql="SELECT * FROM imagetable WHERE ID= '$_POST[search]'";
        $result = mysqli_query($con, $sql);
        if(!mysqli_query($con, $sql)){
            die( "Could not execute sql: $sql"); 
        }
    
        $row = mysqli_fetch_array($result);
        $image_path = $row['file'] . DIRECTORY_SEPARATOR . $row['filename'];  // path should be like this, process/upload/8/cdv_photo_001.jpg
        $image_format = strtolower(pathinfo($image_path, PATHINFO_EXTENSION));
        $pdf->Cell(40,10, $row['sname'],1,0,'c');
        $pdf->Cell(40,10, $row['sadd'],1,0,'c');
        $pdf->Cell(40,10, $row['category'],1,0,'c');
        $pdf->Image($image_path,50,100,50,20,$image_format);
        $pdf->ln();
        $pdf->output();
    }
    
    // your code