2017-04-20 153 views
2

这是我的第一个问题。 我正在尝试使用mpdf从我的数据库的特定行生成pdf。我希望代码在需要时下载特定行的数据。 下载链接位于每行旁边。当下载按钮被按下时,它将下载该行的数据。 代码正在工作,但它正在从数据库中获取所有值并将值并排分配。 这里是我的生成PDF的PHP无法从mysql数据库中获取特定的行

<?php 

//============================================================== 
//============================================================== 
//============================================================== 
include("mpdf/mpdf.php"); 
include "config.php"; 

$res = mysql_query("select date1, date2 from test"); 
if (!$res) 
    die("query error : ".mysql_error()); 
$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
$mpdf->SetDisplayMode('fullpage'); 
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first 
level of a list 
// LOAD a stylesheet 
$stylesheet = file_get_contents('mpdfstyletables.css'); 
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is  
css/style only and no body/html/text 
$html = ' 

<center><h3>Test</h3></center> 
<center> 
<table border="1"> 
<tr> 
<th>Date 1</th><th>Date 2</th> 
</tr> 
<tr>'; 

while($row = mysql_fetch_array($res)){ 
$html .= 
    '<td>'.$row['date1'].'</td> 
    <td>' . $row['date2']. '</td>'; 
} 
$html .= ' 
</tr> 
</table></center> 
'; 
$mpdf->WriteHTML($html,2); 
$mpdf->Output('mpdf.pdf','I'); 
exit; 
//============================================================== 
//============================================================== 
//============================================================== 
?> 

,这里是查看数据库

<html> 
<body> 
<style> 
    table { 
font-family: arial, sans-serif; 
border-collapse: collapse; 
width: 100%; 
} 

td, th { 

text-align: center; 
padding: 8px; 
} 

tr:nth-child(even) { 
background-color: #dddddd; 
} 

    textarea 
{ 
    width:100%; 
} 
.textwrapper 
{ 
    border:1px solid #dddddd; 
    margin:5px 0; 
    padding:1px; 
} 
    </style> 
<?php 
mysql_connect('localhost','root','') or die(mysql_error()); 
mysql_select_db('test') or die(mysql_error()); 
$query=mysql_query("select * from test limit 0,10") or die(mysql_error()); 
echo'<table border="1" ><th >Id</th><th>Date 1</th><th>Date 2</th>'; 
while($res=mysql_fetch_array($query)) 
{ 
    echo'<tr><td>'.$res['id'].'</td><td>'.$res['date1'].'</td> 
    <td>'.$res['date2'].'</td> 
    <td><a href="gen.php?id=<?php echo $row["id"]; ?> Downlaod</a></td> 
    </tr>'; 
} 
echo'</table>'; 
?> 
</body> 
</html> 

Screenshot of my view.php

+3

你需要添加一个where子句来选择你想要的行。 –

+0

添加查询中的条件 – lalithkumar

回答

1

您认为网页的view.php部分,

没有数组名为$row。而是$res保存您的查询的结果集。

用这个代替:

while($res=mysql_fetch_array($query)){ 
    echo"<tr>"; 
     echo "<td>$res['id']</td><td>$res['date1']</td>"; 
     echo "<td>$res['date2']</td>"; 
     echo "<td><a href=\"gen.php?id={$res['id']}\">Download</a></td>"; 
    echo "</tr>"; 
} 

在您的PDF生成代码,更改:

$res = mysql_query("select date1, date2 from test"); 

要:

$res = mysql_query("SELECT date1,date2 FROM test WHERE id=".intval($_GET['id'])); 

请,请,请升级mysql功能mysqli_立即。

我建议您使用prepared statement来保护您的查询免受恶意注入。与此同时,您可以使用intval()作为懒惰/临时修复。

+0

你能给我一个样本pdf生成代码部分?我没有得到它。谢谢。 –

+0

@NemoL对不起,我没有时间复制你的项目。当然你会更快地测试你的代码,因为你已经拥有了所有的零件。 – mickmackusa

+0

当试图查看view.php时,显示**解析错误:语法错误,意外的'id'(T_STRING),期望','或';'**如您所说的更改代码后。 –

相关问题