猿问

获取PDF中的一行?

我正在从 mySQL 表生成 PDF。生成的 PDF 适用于表中的所有值。我希望它特定于单行。例如下图中,我希望为 ID-2 生成 PDF。对于所有其他行也是如此。我附上了两个 php 脚本。fetch.php是我用Generate PDF按钮显示值的generate_pdf.php地方,也是我生成 PDF 的地方。


获取.php


<?php

//fetch.php

$connect = mysqli_connect("localhost", "id12", "", "id12");

$output = '';

$sql = "SELECT * FROM Datas WHERE ID LIKE '%" .$_POST["search"]."%'";

$result = mysqli_query($connect, $sql);


if(mysqli_num_rows($result) > 0)

{

 $output .= '<h4 align = "center">Search Result</h4>';

 $output .= '<div class="table-responsive">

                <table class = "table table bordered">

                   <tr>

                        <th>ID</th>

                        <th>Name</th>

                        <th>Wrongs</th>

                        <th>Rights</th>

                        <th>Percentage</th>

                        <th>Age</th>

                        <th>Generate PDF</th>

                    </tr>';


    while($row = mysqli_fetch_array($result))

    {

        $output .= '

                <tr>

                    <td>'.$row["ID"].'</td>

                    <td>'.$row["Name"].'</td>

                    <td>'.$row["Wrongs"].'</td>

                    <td>'.$row["Rights"].'</td>

                    <td>'.$row["Percentage"].'</td>

                    <td>'.$row["Age"].'</td>

                    <td><form class="form-inline" method="post" action="generate_pdf.php"></td>

                    <td><button type="submit" id="pdf" name="generate_pdf" class="btn btn-primary"><i class="fa fa-pdf"" aria-hidden="true"></i>Generate PDF</button></td>


                </tr>

            ';

    }

    echo $output;


}

else

{

    echo "Data not found";

}


?>

generate_pdf.php


<?php

//include connection file 

include_once("connection.php");

include_once('fpdf/fpdf.php');


class PDF extends FPDF

{

// Page header

function Header()

{


}


// Page footer

function Footer()

{

    // Position at 1.5 cm from bottom

    $this->SetY(-15);

    // Arial italic 8

    $this->SetFont('Arial','I',8);

    // Page number

    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');

}

}

慕工程0101907
浏览 150回答 1
1回答

精慕HU

要回答您的评论,请在 fetch.php 中使用链接按钮和行中的 ID 更改按钮:&nbsp;<tr><td>'.$row["ID"].'</td><td>'.$row["Name"].'</td><td>'.$row["Wrongs"].'</td><td>'.$row["Rights"].'</td><td>'.$row["Percentage"].'</td><td>'.$row["Age"].'</td><td><a href="generate_pdf.php?id='.$row["ID"].'"><button&nbsp; id="pdf" name="generate_pdf" class="btn btn-primary"><i class="fa fa-pdf"" aria-hidden="true"></i>Generate PDF</button></a></td>&nbsp; &nbsp; </tr>为什么要这样做?:此操作允许您将变量从一个页面传递到另一个页面(另一种方法是使用 AJax)在 generate.pdf 中使用 GET 创建变量并在查询中使用它,例如:$id=$_GET['id'];&nbsp;$query = $connString->prepare("SELECT ID, Name, Wrongs, Rights, Percentage, Age FROM Datas WHERE ID=?");$query->bind_param('i',$id);$query->execute();$result=$query->get_result();为什么要这样做?:我们将通过在查询中传递 $_GET 变量来使用它。与您不同,我使用准备好的 stmt 来防止对查询的直接攻击。另一个“提示”更改搜索查询:$search='%'.$_POST["search"].'%';$sql =$connect->prepare("SELECT * FROM Datas WHERE ID LIKE ?");$sql->bind_param('s',$search);$sql->execute();$result = $sql->get_result();为什么要这样做?:正如之前所见,绝对有必要准备查询以防止攻击回答您的最后一条评论:@SimoneRossaini 我忘了提一件事。我的 id 是一个字符串。我如何参考它?由于您提供的参考脚本是整数。Character&nbsp; &nbsp;Descriptioni&nbsp; &nbsp;corresponding variable has type integerd&nbsp; &nbsp;corresponding variable has type doubles&nbsp; &nbsp;corresponding variable has type stringb&nbsp; &nbsp;corresponding variable is a blob and will be sent in packets所以你需要改变 bind_param 像:$query->bind_param('s',$id);
随时随地看视频慕课网APP
我要回答