2

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.
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ishank Sainger
  • 51
  • 1
  • 1
  • 11

1 Answers1

2

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
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • brother it is now opening the pdf but blank no image no data of that row only cells which i have defined in the starting – Ishank Sainger Dec 26 '15 at 21:46
  • and also when iam removing the while loop from the my code for pdf it is showing same error of image extension – Ishank Sainger Dec 26 '15 at 21:54
  • @IshankSainger You're fetching only one row from the table, so there's no need of `while` loop. Also check whether you're getting any row from the table or not, use `mysqli_num_rows()` function for that. I think you're not getting any row from the table. – Rajdeep Paul Dec 26 '15 at 21:57
  • it is showing image file is empty , and one thing $image_path = $row['file']; ----- this file should be path or file name – Ishank Sainger Dec 26 '15 at 21:59
  • @IshankSainger `$image_path = $row['file'];` should have the full path of the image, not just directory or file name, like this `process/upload/8/cdv_photo_001.jpg` – Rajdeep Paul Dec 26 '15 at 22:01
  • but i have tow fields where one is filename in which file exact name is stored and the other name is file where directory name is stored – Ishank Sainger Dec 26 '15 at 22:02
  • @IshankSainger Well, then your `$image_path` should be like this, `$image_path = $row['file'] . DIRECTORY_SEPARATOR . $row['filename'];`. I've updated my answer. – Rajdeep Paul Dec 26 '15 at 22:07
  • thanks a ton buddy again ........... and y iam getting 6 cells y isnt there is 3 cells for table attribute and three cell for there data i guess i have to crrct some order in pdf cells but still thanks a ton – Ishank Sainger Dec 26 '15 at 22:13
  • @IshankSainger You're very welcome. :) Please read [the documentation](http://www.fpdf.org/en/doc/cell.htm) to see how you can format your cells. – Rajdeep Paul Dec 26 '15 at 22:16
  • @IshankSainger Glad to see it was resolved. Good job Rajdeep, *cheers* – Funk Forty Niner Dec 26 '15 at 23:34
  • @Fred-ii- Thanks, happy holidays. :) – Rajdeep Paul Dec 26 '15 at 23:36
  • @RajdeepPaul You're welcome and thanks. Happy Holidays/New Year to you :) – Funk Forty Niner Dec 26 '15 at 23:38
  • @RajdeepPaul http://stackoverflow.com/questions/34493363/dropdown-other-value-not-saving-to-database?noredirect=1#comment56730362_34493363 can u help with this? – Ishank Sainger Dec 28 '15 at 14:38
  • @IshankSainger Sorry, I just saw your question. And I can see that you have solved it already. Cheers. :) – Rajdeep Paul Dec 28 '15 at 15:37
  • @RajdeepPaul ya just did it :) – Ishank Sainger Dec 28 '15 at 15:42