【问题标题】:fpdf error and view table errorfpdf错误和查看表错误
【发布时间】:2015-12-26 21:19:56
【问题描述】:

 $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();

这是两页,出现错误未定义索引:

文件未定义索引:文件所以请尝试指出错误,因为当我添加图像字段时显示主要错误,然后开始弹出错误。

  • 我正在尝试从数据库中获取图像并尝试以 PDF 格式显示它。

【问题讨论】:

  • 您在哪里得到这个错误?指出出现此错误的行
  • 首先关闭你所有的td标签echo "&lt;/td&gt;&lt;td&gt;..."
  • @RajdeepPaul 我在 $file_filename=$_FILES['file']['name']; 的第二页上收到此错误$pdf->Image($image_path,50,100,50,20,$image_format);
  • 您也尝试以这种形式上传文件?您没有文件输入,并且没有正确设置表单来执行此操作。
  • 我正在尝试从数据库中获取图像并尝试以 pdf 格式显示它

标签: php html mysql fpdf


【解决方案1】:

首先,您应该关闭所有&lt;tr&gt;&lt;td&gt; 标签。其次,您没有在表单中发送任何文件,因此您会收到 Undefined index: file 错误,因此请删除这些行,

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

所以在用户点击view 按钮后,您应该像这样处理您的表单并显示图像,

// 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

【讨论】:

  • 兄弟,它现在正在打开 pdf,但没有图像,没有该行的数据,只有我在开始时定义的单元格
  • 并且当我从我的pdf代码中删除while循环时,它显示相同的图像扩展错误
  • @IshankSainger 您只从表中获取一行,因此不需要while 循环。还要检查您是否从表中获取任何行,为此使用 mysqli_num_rows() 函数。我认为您没有从表格中获得任何行。
  • 它显示图像文件为空,并且一件事 $image_path = $row['file']; ----- 这个文件应该是路径或文件名
  • @IshankSainger $image_path = $row['file']; 应该有图片的完整路径,而不仅仅是目录或文件名,像这样process/upload/8/cdv_photo_001.jpg
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
相关资源
最近更新 更多