亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取PDF中的一行?

獲取PDF中的一行?

PHP
慕工程0101907 2022-10-14 15:37:45
我正在從 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 headerfunction Header(){}// Page footerfunction 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');}}
查看完整描述

1 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

要回答您的評論,請在 fetch.php 中使用鏈接按鈕和行中的 ID 更改按鈕:


 <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  id="pdf" name="generate_pdf" class="btn btn-primary"><i class="fa fa-pdf"" aria-hidden="true"></i>Generate PDF</button></a></td>

    </tr>

為什么要這樣做?:此操作允許您將變量從一個頁面傳遞到另一個頁面(另一種方法是使用 AJax)


在 generate.pdf 中使用 GET 創建變量并在查詢中使用它,例如:


$id=$_GET['id'];

 $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   Description

i   corresponding variable has type integer

d   corresponding variable has type double

s   corresponding variable has type string

b   corresponding variable is a blob and will be sent in packets

所以你需要改變 bind_param 像:$query->bind_param('s',$id);


查看完整回答
反對 回復 2022-10-14
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號