Can't Fetch an Particular Row from mysql Database - php

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

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

Related

Changing colour for alternate rows

I want to make a table more readable by alternating row colours. I've been through the other answers but can't see how to apply them to my code. I tried various toggle methods in the last bit of the code but just broke everything.
I can't use jquery or CSS because I can only access this part of the code.
I want to toglle to bgcolor="#8BC6FD"
<table cellspacing=0 cellpadding=2 border="thin">
<col width=150>
<col width=100>
<tr bgcolor="#D4E9FC" style="outline: thin solid">
<td class="viewN"><b>TYPE:- </b></td>
<td class="viewN"><b>PACKING:- </b></td>
</tr>
<?php
$dbconn = pg_connect("host=127.0.0.1 XXXX password=YYYY)") or die('Could not connect: ' . pg_last_error());
$query = "SELECT * FROM stock
ORDER BY name" ;
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td></tr>", htmlspecialchars($myrow['type']),htmlspecialchars($myrow['packing']));
}
?>
</table>
I can't use jquery or CSS because I can only access this part of the code.
It's unclear as to which part of code you're referring. Assuming you can access all the code showed, you can place the <style> tag before or after the <table>.
It's as simple as defining n-th CSS like
<style>
/* tr:nth-child(odd) {background-color: #0000cc} */
tr:nth-child(even) {background-color: #D4E9FC}
</style>

How to create a dynamic table in PHP from SQL

I try to make a dynamic table. The data comes from a database.
It works so far, but I want to make a table with seperate fields, not like my way.
My code so far:
<?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result);
echo("Buy Date"."|Expire Date");
echo "<br />";
echo "<br />";
while ($row = mysql_fetch_array($result)) {
echo '<th>'.$row['buyTime'].'</th>'."|".'<th>'.$row['untilTime'].'</th>';
echo "<br />";
}
?>
The result:
Click here
So how can I make a correct table, not a pseudo one?
Thank you :)
Regards
Use a table element on html and put your php loop code inside the tbody element. How to properly create a table;
<table>
<thead>
<tr>
<th>Buy Date</th>
<th>Expire Date</th>
</tr>
</thead>
<tbody>
<?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$row['buyTime'].'</td><td>'.$row['untilTime'].'</td>';
echo '</tr>'
}
?>
</tbody>
</table>
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h2>Colored Table Header</h2>
<table>
<tr>
<th>Buy Date</th>
<th>Expire Date</th>
</tr>
<?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row['buyTime']; ?></td>
<td><?php echo $row['untilTime']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
NOTE: An HTML table is defined with the <table>tag.
Each table row is defined with the <tr> tag. A table header is defined with the <th> tag. By default, table headings are bold and centered. A table data/cell is defined with the <td> tag. Moreover Mysql is outmoded please try learn new approach. You can learn it on w3schools php
Good Luck!!!

How to style a table that is part html and part php

I found it hard to phrase this question but I will try my best. I am creating a php and MYSQL leaderboard, it all works but now I want to style it. I don't know very much css and this might be a simple fix. I am trying to style the table so that all the data table elements sit symmetrical like a table should be, and aligned to the center of the headers. I think it is not working because I technically have 2 tables I am trying to style and they are not working in conjunction with each other. Here is the code and forgive my usernames in the table, I get lazy sometimes.
<!DOCTYPE html>
<html>
<head>
<title>Leaderboards</title>
<style type="text/css">
th {
overflow: auto;
font-size: 25px;
border: 1px solid;
padding: 5px;
margin: 2px 2px 2px 2px;
}
td {
font-size: 25px;
padding-left: 30px;
padding-top: 3px;
}
</style>
</head>
<body>
<h1>Leaderboard</h1>
<table>
<tr>
<th>Rank</th>
<th>User</th>
<th>Score</th>
</tr>
</table>
<?php
include 'HytecFunctions.php';
$conn=connectDB();
$rank = 1;
$sql = 'SELECT Name, Score FROM Names ORDER BY Score DESC';
foreach ($conn->query($sql) as $row) {
echo "<table>
<td>$rank</td>
<td>$row[Name]</td>
<td>$row[Score]</td>
</tr>
</table>";
$rank++;
}
$conn->close();
?>
</body>
</html>
The table as it shows in my browser
Because you try to create another table every loop. Try to put them all in a single table.
<table>
<tr>
<th>Rank</th>
<th>User</th>
<th>Score</th>
</tr>
<?php
include 'HytecFunctions.php';
$conn=connectDB();
$rank = 1;
$sql = 'SELECT Name, Score FROM Names ORDER BY Score DESC';
foreach ($conn->query($sql) as $row) {
echo '<tr>
<td>'.$rank.'</td>
<td>'.$row["Name"].'</td>
<td>'.$row["Score"].'</td>
</tr>';
$rank++;
}
$conn->close();
?>
</table>
Note: Don't mind much the way I input your $row variables. Yours will still work even if you use double tick (") to display rows. I just use a single tick (') to display your data as is to have a much cleaner look on your code. Refer here for the difference.

Table not formatting correctly on safari

I have the following php code, which gathers data from a database, and displays it in a table. It works fine when used in chrome, But is not working properly, i.e, the border, rows etc.. are not being displayed, like the table tag itself is being disregarded. Here is the code, I have.
<html>
<head>
<title> Reviews List. </title>
</head>
<body>
<?php
include 'settings.php';
if (!isset($dbc)){
$dbc = new mysqli(DB_HOST , DB_USER , DB_PASSWORD , DB_NAME) or die("Cannot connect to the database.");
}
$query = "SELECT * FROM reviews";
$result = $dbc->query($query) or die ("Cannot query");
?>
<table id="review_table" border="1" width="1">
<?php
while ($row = mysqli_fetch_assoc($result)){
echo '<tr>';
echo '<td>';
echo $row['id'];
echo '</td>';
echo '<td>';
echo $row['pid'];
echo '</td>';
echo '<td>';
echo $row['publish'];
echo '</td>';
echo '</tr>';
}
?>
</table>
</body>
Here are the screen shots of the page viewed in safari and in chrome.
Safari:
I did try to click on the toggle formatting button, which did nothing.
Chrome:
Remove border and width from table tag and add this css instead.
#review_table {
border: 1px solid #000;
border-collapse: collapse;
}
#review_table td {
border: 1px solid #000;
}
You are missing the <tbody> tag

give color to specific row of a table acc. to situation

I created a table from database column name "Do You have passport" user answers in yes or no I want where user answer is yes that row should be green and row where user answer is no that row is white can any one tell me how can I apply css to this table that works dynamically.
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
</html>
<?php
$conn = new mysqli("localhost","root","","db_dat");
$havepassport='';
$sql="SELECT * from upload;
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$havepassport.='<table>'.'<tr>'.'<td>';
$havepassport.=$row['having_passport'];
$havepassport.='</table>'.'</tr>'.'</td>';
echo $havepassport;
}
?>
You are doing it wrong. Your while add new table to HTML. So, if you have 100 rows, 100 tables will be added to DOM instead of rows.
Use following:
PHP
$sql = "SELECT * from upload";
$result = $conn -> query($sql);
$havepassport = '<table>';
while ($row = $result -> fetch_assoc()) {
$passportClass = $row['having_passport'] == 'Yes' ? 'green' : 'red';
// ^^^^^^^^^^^ Getting classname depending on the passport value
$havepassport .= '<tr class='.$passportClass.'>'.
// ^^^^^^^^^^^^^^ Add Class Here
'<td>';
$havepassport. = $row['having_passport'];
$havepassport .= '</td>'.
'</tr>';
}
$havepassport .= '</table>';
echo $havepassport;
CSS:
.green {
background: green;
}
.red {
background: red;
}
Try this:
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
</html>
<?php
$conn = new mysqli("localhost","root","","db_dat");
$havepassport='';
$sql="SELECT * from upload;
$result = $conn->query($sql);
$havepassport.='<table>';
while($row = $result->fetch_assoc()) {
$passHaveClass = '';
if($row['having_passport'] =="yes"){
$passHaveClass = "greenColor";
}
$havepassport.='<tr class='.$passHaveClass.'>';
$havepassport.= '<td>'.$row['having_passport'].'</td>';
$havepassport.='</tr>;
}
$havepassport.='</table>';
echo $havepassport;
?>
the greenColor class having bg color is green.
checkout this fiddle : https://jsfiddle.net/knkp02Ld/1/
HTML
<table id="mytable">
<tr>
<td>User Name</td>
<td>Yes</td>
</tr>
<tr>
<td>User id</td>
<td>No</td>
</tr>
<tr>
<td>Whatever</td>
<td>Yes</td>
</tr>
</table>
JS
$('#mytable td:contains("Yes")').parent().css('background', 'green');
$('#mytable td:contains("No")').parent().css('background', 'red');
Select all elements that contain the specified text.
docs: https://api.jquery.com/contains-selector/
Edit
Other way you can use .each function like as follows
$('#mytable td').each(function() {
var value = $(this).html();
if (value == "Yes") {
$(this).parent().css("background", "red");
}
});

Categories