PHP - empty table appearing after first result from nested while loop - php

I'm getting a table appearing after the first result of the nested while loop.
What I'm trying to do is display the usernames as a list by selecting from the "users" table, then comparing that list with the project engineers from the "current projects" table.
This way there is a list of project engineers with their assigned projects underneath their names.
Unfortunately, I'm after the first nested while loop runs, it spits out a empty table and I'm not sure how to get rid of it.
I'm suspecting it has to do with the initial variable of $proj_engineer = "";
Does anyone know where this empty table is coming from and how to get rid of it?
Below is the code:
<?php
$query = mysql_query("SELECT * FROM `users` ORDER BY `username` ASC") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$user_id = $row['user_id'];
$username = $row['username'];
$proj_engineer = "";
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_engineer` ASC") or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) {
$proj_id = $row1['proj_id'];
$proj_engineer = $row1['proj_engineer'];
}
echo "<table border=1><tr><td colspan=12><p class='bold18'>" . $proj_engineer . "</p></td></tr>";
$query3 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_id` DESC") or die(mysql_error());
while ($row3 = mysql_fetch_assoc($query3)) {
$proj_id = $row3['proj_id'];
$proj_number = $row3['proj_number'];
$proj_name = $row3['proj_name'];
$proj_sort = $row3['proj_sort'];
$proj_start = $row3['proj_start'];
$proj_finish = $row3['proj_finish'];
$proj_overstat = $row3['proj_overstat'];
$proj_dwgstat = $row3['proj_dwgstat'];
$proj_soostat = $row3['proj_soostat'];
$proj_substat = $row3['proj_substat'];
$proj_engineer = $row3['proj_engineer'];
$proj_drafter = $row3['proj_drafter'];
$proj_rating = $row3['proj_rating'];
$proj_pending = $row3['proj_pending'];
$proj_notes = $row3['proj_notes'];
$start_time = date("m/d/y", $proj_start);
$finish_time = date("m/d/y", $proj_finish);
echo "
<tr>
<td width=40>$proj_number</td>
<td width=100>$proj_engineer</td>
<td width=100><a href='./project-page.php?proj_id=$proj_id'>$proj_name</a></td>
<td width=40>$start_time</td>
<td width=40>$finish_time</td>
<td width=110>
<div style='position:relative; background:url(images/bar01.gif); width:$proj_overstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_overstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_overstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar02.gif); width:$proj_dwgstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_dwgstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_dwgstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar03.gif); width:$proj_soostat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_soostat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_soostat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar04.gif); width:$proj_substat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_substat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_substat%</div>
</div>
</td>
<td width=40 align='center'><a href='project-notes.php?proj_id=$proj_id'><img src='images/note.png' border=0></a></td>
<td width=40 align='center'><a href='./project-edit.php?proj_id=$proj_id'>EDIT</a></td>
<td width=40 align='center'><a href='./project-delete.php?proj_id=$proj_id'>DELETE</a></td>
</tr>
";
}
echo "</table><br>";
}
?>

This might be because, your query: -
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE
`proj_engineer`='$username' ORDER BY `proj_engineer` ASC")
or die(mysql_error());
fetched you an empty value for $proj_engineer. You can do a check before printing your table, whether your variable contains a value instead. Try printing the $proj_engineer before printing the table.
You can enclose your code echoing the table inside an if construct, which will print table only when your value is not empty.

Related

display unrelated data from 2 tables

Trying to get data to display from 2 different and basically unrelated tables. Don't want them joined, just discrete output to look like this in a select box:
Category: current category from table1 (this part works)
Entire list of categories from table 2 (correct number of empty option tags, no data showing)
DB connection code:
//DB CONNECTION//
$dbcnx = mysqli_connect("localhost", $DBASEUSER, $DBASEPASSWORD, $DBASE);
//Main Showcase
$sql = "SELECT * FROM `dprods`";
//////HEADER in main page
<?php
$ID=$_GET['ID'];
$CAT=$_GET['cat'];
?>
//////Main SECTION
<?php
require "db_conn.php";
$sql2 = "SELECT * FROM $DBTABLE WHERE ID=$ID";
$getinfo = mysqli_query($dbcnx, $sql2);
$row = mysqli_fetch_assoc($getinfo);
$PROD = $row['dtitle'];
$PRICE = $row['dprice'];
$PP = $row['dpplink'];
echo "<tr>
<td width=\"12%\">
<div align=\"right\"><b><font face=\"Arial, Helvetica, sans-serif\" size=2>Product
Category: </font></b></div>
</td>
<td colspan=2 width=\"88%\"><select name=\"dcat\"><option value=\"$CAT\">Current Category: $CAT</option>";
$dcat = "SELECT * FROM 'dcat'";
$getcat = mysqli_query($dbcnx, $dcat);
$row2 = mysqli_fetch_assoc($getcat);
while($row2 = mysqli_fetch_assoc($result))
{ echo " <option value=\"".$row2["dcategory"]."\">".$row2["dcategory"]."</option>";
}
echo "".$row["dprice"]."</select>
</tr>
<tr>
<td width=\"12%\"> </td>
<td width=\"24%\"> </td>
<td width=\"64%\"> </td>
</tr>
<tr>
<td width=\"12%\">
<div align=\"right\"><b><font face=\"Arial, Helvetica, sans-serif\" size=2>Product
Title: </font></b></div>
</td>
<td width=\"24%\"> <b><font face=\"Arial, Helvetica, sans-serif\" size=2>
<input type=\"text\" name=\"Title\" value=\"".$row["dtitle"]."\" size=35 maxlength=25>
</font></b><b><font face=\"Arial, Helvetica, sans-serif\" size=2> </font></b></td>
<td width=\"64%\"><b><font face=\"Arial, Helvetica, sans-serif\" size=2>Price</font></b>:
<b><font face=\"Arial, Helvetica, sans-serif\" size=2>
<input type=\"text\" name=\"Price\" value=\"".$row["dprice"]."\" maxlength=10 size=20>
</font></b></td>
</tr>
<tr>
<td width=\"12%\"> </td>
<td width=\"24%\"> </td>
<td width=\"64%\"> </td>
</tr>
<tr>
<td width=\"12%\">
<div align=\"right\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=2><b>PayPal
Link: </b></font></div>
</td>
<td colspan=2><font face=\"Arial, Helvetica, sans-serif\"><b>http://www.paypal.com</b>
</font>
<input type=\"text\" name=\"PP\" value=\"".$row["dpplink"]."\" size=50>
<font face=\"Arial, Helvetica, sans-serif\">Only put in what follows paypal.com</font></td>
</tr>
";
mysqli_close($dbcnx);
?>
Been working and researching this for hours and now I'm so horribly confused, lupus induced Swiss-cheese brain isn't helping. I think I'm very close, just unable to get the data from the 2nd table to display. Would appreciate any suggestions, but please keep it as simple as possible. Some of the complex code I've seen in my research confuses me even more. Thanks!
Try including getcat in your select query like this:
$dcat = "SELECT 'dcategory', 'getcat' FROM 'dcat'";
$getcat = mysqli_query($dbcnx, $dcat);
while($row2 = mysqli_fetch_assoc($getcat))
{
echo " <option value=\"".$row2["getcat"]."\">".$row2["getcat"]."</option>";
}
echo "</select>"
You could also use $dcat = "SELECT * FROM 'dcat'";, but the above is likely slightly more efficient.
Also, I don't see where $result was being set for this code while($row2 = mysqli_fetch_assoc($result))
It looks like $result isn't set, so it would obviously give you undesirable results.
You're also doing $row2 = mysqli_fetch_assoc($getcat); outside of the while loop, which will grab the first row before entering the loop. The loop would start at the second row.
Please look at http://php.net/manual/en/mysqli-result.fetch-assoc.php
Also, your script will be subject to SQL Injection, as mentioned in the comments. You will need to fix it so it is as safe as possible. Read the links in the comments and also make sure you're using prepared statements
adpro, your help was invaluable. I was more confused than I realized and your comments helped point that out to me. I went back & started from scratch with a clean query code that I could print out, mark up with colored hi-liters to follow the variables. Code now works as desired and I'm posting for anyone else who may need the clarification. I appreciate the input from everyone about the sql injection. My goal was to get the simple functionality working first before making it more complicated. Now I can add the protection to prevent sql injection.
<?php
require "db_conn.php";
$sql2 = "SELECT * FROM $DBTABLE WHERE ID=$ID";
$getinfo = mysqli_query($dbcnx, $sql2);
$row = mysqli_fetch_assoc($getinfo);
$PROD = $row['dtitle'];
$PRICE = $row['dprice'];
$PP = $row['dpplink'];
echo "<tr>
<td width=\"12%\">
<div align=\"right\"><b><font face=\"Arial, Helvetica, sans-serif\" size=2>Product
Category: </font></b></div>
</td>
<td colspan=2 width=\"88%\"><select name=\"dcat\"><option value=\"$CAT\">Current Category: $CAT</option>";
$sqlCAT = "SELECT * FROM dcat";
if($resultCAT = mysqli_query($dbcnx, $sqlCAT)){
if(mysqli_num_rows($resultCAT) > 0){
while($rowCAT = mysqli_fetch_array($resultCAT)){
echo "<option value=\"".$rowCAT['dcategory']."\">".$rowCAT['dcategory']."</option>";
}
// Free result set
mysqli_free_result($resultCAT);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sqlCAT. " . mysqli_error($dbcnx);
}
echo " </select>";
mysqli_close($dbcnx);
?>

PHP Variable not Echoing

Evening All, just trying to figure out why some of my variables are not echoing from my MYSQL database. At first they were going to be Session variables of which i found did not work, so then i changed to a mysql query on my invoice page
$OrderID = $_SESSION[OrderID];
$LName = mysql_result(mysql_query("SELECT LName FROM customers WHERE Username = '$Username'"), 0);
$Fname = mysql_result(mysql_query("SELECT FName FROM customers WHERE Username = '$Username'"), 0);
$Date = mysql_result(mysql_query("SELECT Date FROM orders WHERE OrderID = '$OrderID'"), 0);
My Queries above. Order ID and Date are working, yet Lname and Fname are not where everything is named correctly as they are in the database.
Full Code below of where i am trying to echo them.
<div id='containter' style='position:absolute;width:750px;height:800px; left:50% ; margin-left: -375px; border-style: solid;border-width: 1px;'>
<div id='heading' style='position:absolute;width:650px;height:100px; left:50% ; margin-left: -325px;top:20px; border-style: solid;border-width: 1px;'>
<div style='position:absolute;left:500px;text-align:center;font-size:36px;'> <p>INVOICE </p></div>
<div style='position:absolute;text-align:center;font-size:38px;width:250px;left:-20px;'> OzScopes</div>
<div style='position:absolute;text-align:center;font-size:16px;width:250px;left:-37px;top:45px;'> ozscopes.com.au</div>
<div id='headingbottom' style='position:absolute;width:100%;height:20px;top:80px;'>
<table>
<tr>
<td style='position:absolute;left:5%;'>Invoice Number </td>
<td style='position:absolute;left:25%;'> <?php echo $OrderID;?> </td>
<td style='position:absolute;left:77%;'><?php echo $Date ;?></td>
</tr>
</table>
</div>
</div>
<div id='invoicecontent' style='position:absolute;width:650px;height:600px; left:50% ; margin-left: -325px;top:140px; border-style: solid;border-width: 1px;'>
<div id='information'>
<div id='customerinfo' style='position:absolute;width:255px;height:75px;top:-210px;padding:10px; border-bottom: 2px solid;border-right:1px solid; display:inline'>
<table>
<tr>
<th>Customer Information</th>
</tr>
<tr>
<td> <?php echo $LName;?> lnametest</td>
</tr>
</table>
</div>
<div id='mailinginfo' style='position:absolute;width:355px;height:75px;left:275px;top:-210px;padding:10px;border-bottom: 2px solid; display:inline'>
Mailing Information
</div>
</div>
<div id='products' style='position:absolute;top:97px;width:650px;height:503px;'>
<?php
echo "<table border=\"0\" cellspacing=\"0\" padding=\"0\" width=\"650px\">";
echo "<tr style='font-weight: bold; font-size:16px;'>";
echo "<td colspan=\"2\" align=\"center\" style='border-bottom: 2px solid;font-family:'Ek Mukta';color:#464d48;'>Description</td>";
echo "<td align=\"left\" style='border-bottom: 2px solid;font-family:'Ek Mukta';color:#464d48;'>Quantity</td>";
echo "<td colspan=\"3\" align=\"center\" style='border-bottom: 2px solid ;font-family:'Ek Mukta';color:#464d48;'>Price (Per Unit)</td>";
echo "<td align=\"center\" style='border-bottom: 2px solid;font-family:'Ek Mukta';color:#464d48;'>Total</td>";
echo "</tr>";
echo "</table>";
?>
</div>
<div id='payment'>
</div>
</div>
</div>
Where are you getting $Username? should you get it from the session first?
Like so:
$Username = $_SESSION[Username];
otherwise this string is empty and the select statement will come back as empty, because the where clause was based on username = $Username (which in this case is empty).
Get username of the session variable first, then use it in where clause. Apart from that, as others have said, please avoid using mysql queries, use mysqli instead.
Hope it helps.

Increment a value in a sql database when a pdf link is downloaded on a webpage

I'm creating an eBook account on my website: where customers can have a library of ebooks and every time an eBook is downloaded, I need to add 1 to the database for the client to be able to see the number of downloads. I can not figure out the line of code I would need for this and I have done research but nothing can help with this specific query. This is what I have so far, which pulls the information from a sql table and display in html table:
<p>List of Publications</p>
<?php
//connect
$query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<td> <?php if ($numrows > 0){
while($row = mysql_fetch_array($query))
{
?>
<a href="<?php echo $row['Link']; ?>" target="_blank">
<img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></a> <?php
}
} else
echo "Wrong Query";
?>
</tr>
You could use JQuery
ebookDownloads.php
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="javascript/text">
// JavaScript Document
function Downloaded(id) {
var data = {'id':id};
$.post( "downloaded.php", data);
}
</script>
</head>
<p>List of Publications</p>
<?php
//connect $query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<?php
if ($numrows > 0){
while($row = mysql_fetch_array($query)) {
?>
<td><img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></td>
<?php
}
} else {
echo "Wrong Query";
}
?>
</tr>
</table>
downloaded.php
if(isset($_POST['id']) && is_numeric($_POST['id'])) {
$sql = "UPDATE table SET downloaded=downloaded+1 WHERE id = $_POST['id']";
}
EDIT: I have not tested this code. Also the user might not have javascript enabled in which case it wouldn't count the download, so if you need to count the downloads (e.g they pay for so many) you could make sure they have javascript enabled before showing the downloads.
Update: another method using a new tab and PHP to record the download then present it
<p>List of Publications</p>
<?php
//connect $query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<?php
if ($numrows > 0){
while($row = mysql_fetch_array($query)) {
?>
<td><img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></td>
<?php
}
} else {
echo "Wrong Query";
}
?>
</tr>
</table>
<?php
######## download.php #######################
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$query = "SELECT * FROM ebooks WHERE id = $_GET['id']";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
$sql = "UPDATE table SET downloaded=downloaded+1 WHERE id = $_POST['id']"; // update downloaded
while($row = mysql_fetch_array($query)) {
header('Content-Type: application/pdf');
header('Content-disposition: attachment;filename='.$row['name'].'pdf');
readfile($row['link']); // should be an absolute path
}
}
}
?>

Add value to table cell using PHP

I am trying to create a class record. I created a table, with column of names and activities.
the table looks like this.
Name | Q1 | Q2 |
--------------------------------
Prinz | 20 | (input text)
My aim is to save the input data into my database, but I need to have the id of the student(Prinz). I already managed to save the grade but want the id of that particular student to be save as well. My MYSQL table has 4 att. namely, grade_id(auto_inc), stud_id, grade and the activity type(Quiz, Assignment, Exam).
Here's my code
<table border="0" cellspacing="0" cellpadding="0" style="width: 845px; margin-top: 20px; margin-bottom: 65px;">
<tr style="background-color: #caccca; text-align: center;">
<td style="width: 130px;" class="td-1">NAME</td>
<?php
$result = mysql_query("select * from subject_grade ");
$numrows = mysql_num_rows($result);
while ($row = mysql_fetch_object($result)) {
$act_type = $row->act_type;
echo '<td style="width: 130px;" class="td-1">'.$row->act_type .'</td>';
}
?>
<td class="td-1">GPA</td>
</tr>
<?php
$result = mysql_query("select * from student_db ");
while ($row = mysql_fetch_object($result)) {
echo '<tr class="border" style="background-color: #f9f9f9;"><td style="width: 150px;" class="td-2" name="student"><p>'.$row->firstname .' '. $row -> lastname .'</p></td>';
$id = $row->student_id;
for ($i=0; $i<$numrows; $i++){
echo '<form action ="record.php" method="POST"><td style="text-align: center;"><input type="text "style="font-size: 12px; width: 20px;" name="grade"><input style="display: none;" type="text" name="id" value="'.$id.'"><input type="submit"></td></form>';
}
echo '<td style="width: 80px;"class="td-2"></td>';
}
?>
</tr>
</table>
RECORD.PHP File
if($_POST['grade']){
$sql="INSERT INTO class_record (grade, student_id)
VALUES
('$_POST[grade]','$_POST[id]')";
$rs1 = mysqli_query($con,$sql);
echo $_POST['grade'] .' '. $studID;
} else echo 'ERROR';
?>
I hope you can help me, this is for our school project.
What data type are you storing student_id as?
('$_POST[grade]','$_POST[id]')";
use this query
$sql="INSERT INTO class_record (grade, student_id)
VALUES
('".$_POST['grade']."','".$_POST['id']."')";

PHP echo tables

Overview
I have some data stored in MySql database related to Products. I am trying to retrieve this data and display it on a page using HTML table.
The PHP and MySql has gone well and all the data is retrieved but it is displayed in a very messy manner.
Here is what I have as a layout:
What I am aiming to achieve is to further divide the results table add more columns rows to make the data more readable
Something like this;
The code: PHP, MySQL & HTML:
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products");
echo "<table id='display'>";
while($rows = mysql_fetch_array($sql))
{
echo"<br>";
echo"<tr><td>";
echo"$rows[$product_name]<br></td>";
echo"<td><img src=$rows[$product_image] height='200px' width='200px'><br></td>";
echo"<td>Avalible: $rows[$product_qua]<br></td>";
echo"<td>Price: $rows[$product_price]<br></td>";
echo"<td>Description: $rows[$product_des]<br></td>";
echo"</tr>";
}
echo "</table>";
?>
CSS responsible for this part:
#display{
float:left;
border: 5px solid black;
margin-left:100px;
}
just add some padding or a border to the table cells:
table#display td{
border: 1px solid black;
padding:0 8px;
}
Edit: What you could do as well:
<table id='display'>
<?php while($rows = mysql_fetch_array($sql)): ?>
<!-- <br> <- why a break? it's not in the correct spot anyway -->
<tr><td>
<?php echo $rows[$product_name]; ?><br>
</td>
<td> - </td>
<td><img src="<?php echo $rows[$product_image]; ?>" height='200px' width='200px'><br></td>
<td> - </td>
<td>Avalible: <?php echo $rows[$product_qua]; ?><br></td>
<td> - </td>
<td>Price: <?php echo $rows[$product_price]; ?><br></td>
<td> - </td>
<td>Description: <?php echo $rows[$product_des]; ?><br></td>
</tr>
<?php endwhile; ?>
</table>
Tip: I prefer to use the while/endwhile; approach rather than using brackets when displaying data to the user.
First of all don't echo so much HTML using PHP, instead do it like this
<?php
session_start();
include('connect_mysql.php');
$product_name = 'product_name';
$product_qua = 'product_qua';
$product_price = 'product_price';
$product_image = 'product_image';
$product_des = 'product_des';
$sql = mysql_query("SELECT * FROM products"); ?>
<table id='display'>
<?php
while($rows = mysql_fetch_array($sql)) {
?>
<tr><td><?php echo $rows[$product_name]; ?></td>
<!-- And so on-->
<?php
}
?>
</table>
Secondly by seeing your inmage, it seems like you need a border for your table so use
table, td {
border: 1px solid #000000;
}
add the following css to apply border on your table cells:
#display td{ border: 2px solid red; }
and optionally add to your #display { :
border-collapse: collapse;

Categories