2

this is my first question here. I am trying to generate pdf from a particular row of my database using mpdf. I want the code to download a particular row's data when needed. A download link is beside every row. When the download button is pressed it will download that row's data. The code is working but it is fetching all the values from the db and assigning the values side by side. Here is my generate pdf php

<?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;
//==============================================================
//==============================================================
//==============================================================
?>

And Here is the view.php part for viewing the database

<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

2
  • 3
    You need to add a where clause to select the row you want. Commented Apr 20, 2017 at 10:45
  • add where condition in query Commented Apr 20, 2017 at 10:46

1 Answer 1

1

In your view page,

There is no array called $row. Rather $res holds your query's resultset.

Use this instead:

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>";
}

In you pdf generating code, change:

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

To:

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

Please, please, please upgrade your mysql functions to mysqli_ immediately.

I recommend using a prepared statement to protect your query from malicious injection. In the meantime, you can use intval() as a lazy/temporary fix.

Sign up to request clarification or add additional context in comments.

8 Comments

Can you give me a sample pdf generate code part? I'm not getting it. Thanks.
@NemoL Sorry, I don't have the time to replicate your project. Surely you will be quicker to test your code because you already have all the bits and pieces.
When trying to view the view.php, its showing Parse error: syntax error, unexpected 'id' (T_STRING), expecting ',' or ';' after changing the code as you said.
@NemoL on which line? Does your url say ?id=something? Can you echo $_GET['id'] before the query? You may need to make some pastebin links again so I can scrutinize your updates.
The quoting needs to be adjusted to permit variables in the string. Change: echo'<tr><td>'.$res['ID'].'</td><td>'.$res['date1'].'</td><td>'.$res['date2'].'</td> <td><a href=\"gen.php?id={$res['id']}\">Download</a></td> </tr>'; to echo "<tr><td>{$res['ID']}</td><td>{$res['date1']}</td><td>{$res['date2']}</td><td><a href=\"gen.php?id={$res['id']}\">Download</a></td></tr>";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.