showing contents of a table using PHP in HTML - php

I have this code snippet:
try
{
$conn = new PDO("mysql:host=$host;dbname=judges", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
and
$query = "SELECT * FROM feedback";
echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial">Judge Name</font> </td>
<td> <font face="Arial">PC</font> </td>
<td> <font face="Arial">TD</font> </td>
<td> <font face="Arial">EX</font> </td>
<td> <font face="Arial">ID</font> </td>
<td> <font face="Arial">comment</font> </td>
</tr>';
if ($result = $conn->query($query)) {
echo "got result";
echo $result["PC"];
//while ($row = $result->fetch_assoc()) {
while ($row = $result) {
$field1name = $row["judgeName"];
$field2name = $row["PC"];
$field3name = $row["TD"];
$field4name = $row["EX"];
$field5name = $row["ID"];
$field6name = $row["comment"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
}
both inside submit.php, however, while the feedback table inside judges database is not empty, I don't see any of the rows shown in the HTML page only the column name shows up in the Response tab of Network tab in Firefox Inspect tool. What do you suggest to populate the existing database into the html page even before I enter the information for the new user?
Here's my feedback table from the judges database:
Trying the following from the comments also didn't work and nothing got printed:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

There's no point in the if statement. You have PDO::ERRMODE_EXCEPTION enabled, so if there's an error in the query it will throw an exception, not return false.
The syntax to fetch a row in PDO is $result->fetch(PDO::FETCH_ASSOC).
$result = $conn->query($query);
echo "<td><tr>got result</tr></td>";
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$field1name = $row["judgeName"];
$field2name = $row["PC"];
$field3name = $row["TD"];
$field4name = $row["EX"];
$field5name = $row["ID"];
$field6name = $row["comment"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}

You were mixing PDO and MYSQLI_
Also you had some debug code that would have damaged your table HTML.
Also there is no real point in moving data from a perfectly good array into scalar variables just to include those values in some HTML.
if ($result = $conn->query($query)) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>$row[judgeName]</td>
<td>$row[PC]</td>
<td>$row[TD]</td>
<td>$row[EX]</td>
<td>$row[ID]</td>
<td>$row[comment]</td>
</tr>";
}
}

Related

$result & $search in same PHP file

I want to search with PHP in mysql Database, and search results to edit or delete, but until now i can do search and everything works or Edit/Delete and search dosen't work anymore..
I want to combine both in same php file, but dosen't work. Know someone how to combine ?
How can i add both in same php.. ?
Search PHP
<?php
include_once("db_W3.php");
$result = mysqli_query($mysqli, "SELECT * FROM centralhub_lagerbestand");
//load database connection
include_once("db_W3.php");
$host = "$servername";
$user = "$username";
$password = "$password";
$database_name = "$db";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from centralhub_lagerbestand where KundenNr LIKE '%$search%' LIMIT 0 , 50");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "<table style=\"font-family:arial;color:#333333;\">";
echo "<tr>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Kunden Nr.:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Regal:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Regal Ebene:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\"> Edit </td>
</tr>";
while ($results = $query->fetch()) {
echo "<tr> <td width='500px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo $results['KundenNr'];
echo "</td><td width='230px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo $results['Regal'];
echo "</td><td width='230px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo "".$results['RegalEbene'];
echo "</td> </tr>";
}
echo "</table>";
} else {
echo 'Für die aktuellen Filter wurden keine Ergebnisse gefunden';
}
?>
Edit/Delete PHP
<?php
//including the database connection file
include_once("db_W3.php");
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM centralhub_lagerbestand ORDER BY id DESC"); // using mysqli_query instead
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<table width='100%' border=0>
<tr bgcolor='#CCCCCC'>
<td>KundenNr</td>
<td>Regal</td>
<td>RegalEbene</td>
<td>Update</td>
</tr>
<?php
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['KundenNr']."</td>";
echo "<td>".$res['Regal']."</td>";
echo "<td>".$res['RegalEbene']."</td>";
}
?>
</table>
</body>
</html>

PHP - Inputting data into MySQL cell

I am trying put row['user'] into cell but it doesn't work.
When I uncomment "echo" it works fine.
PHP:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html = $html . '<tr><td>' . $row['user'] . '</td></tr>';
// echo $row['user'];
}
return $html;
}
}
I also have a HTML view file where I have:
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
%mod_Something%
</tbody>
</table>
I know that HTML isn't a function but I must return it because there is a script which allows to return "view".
try this:
function mod_Something($database)
{
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>' . $row['user']; . '</td></tr>';
}
return $html;
}
}
<table id="data-table-basic" class="table table-striped">
<thead>
<tr>
<th>user</th>
</tr>
</thead>
<tbody>
<?php mod_Something(); ?>
</tbody>
</table>
You are not getting anything from the function mod_Something because you are not building & returning HTML properly.
See the below code.
function mod_Something($database)
{
$html = "";
$sql = "SELECT user FROM table_name";
if ($result = mysqli_query($database, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
$html .= '<tr><td>' . $row['user'] . '</td></tr>';
}
}
return $html;
}
Hope this works for you!

Displaying data in tables depending on group

I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.
What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png
What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png
<div class="col-lg-6">
<h3>Team Name Goes Here </h3>
<?php
echo "<table class='table table-striped'>";
echo " <thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
";
while($row = mysqli_fetch_array($result)) {
$teamName = $row['teamName'];
$fName = $row['firstName'];
$surName = $row['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
?>
</div>
with my query :
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ";
I know the idea I need to do but cannot get it done, so any help would be appreciated.
Try this code
<?php $teemid=array();
while($row = mysqli_fetch_array($result)) {
if(!in_array($row['teamID'],$teemid)){
array_push($teemid,$row['teamID']);
if(!empty($teemid)){ ?>
</tbody>
</table>
</div>
<?php }
?>
<div class="col-lg-6">
<h3><?php echo $row['teamName']; ?></h3>
<table class='table table-striped'>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $row['teamName']; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['surName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
SQL Query Change as below
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ORDER BY u.teamID";
You can do this logic
$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
$teamid = $row['teamid'];
$teamname = $row['teamname'];
$teammemberquery = "select all member in the where team = $teamid sql query";
echo "<table>";
while ($r = mysqli_fetch_array($teammemberquery)) {
$teamName = $r['teamName'];
$fName = $r['firstName'];
$surName = $r['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
}
Try as below (Please replace table column name as yours and mysql to mysqli):
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
<?php
echo "<table>";
$teamName = "";
$i=0;
while($row = mysql_fetch_array($result))
{
if($teamName == "" || $teamName != $row['team'])
{
if($i!=0)
echo "</table>";
echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
$teamName = $row['team'];
$i=0;
}
$fName = $row['fname'];
$surName = $row['lname'];
$email = $row['email'];
if($i==0)
{
echo "<table class='table table-striped'><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
}
echo "<tr>
<td>$fName</td>
<td>$surName</td>
<td>$email</td>
</tr>";
$i++;
}
echo "</table>";
?>
</div></body></html>

Don't display if no records exist in mysql

I have this form to search names in mysql database
<form action="search.php" method="GET">
<input type="text" placeholder="Search" name="name">
<input type="submit" value="Search">
this is the search.php
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
}
echo
"</tbody>
</table>";
?>
so there is no problems when I search for a name that exists in database it displays correctly, but the problem comes when I search for a name that doesn't exist in database.. I want it to display only
"No data available for that name specified" for the output but I will also see empty table in the output like this ------------> IMAGE..
so how can I get rid of the empty table for the output?
Just pu the if outside the table....
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
?>
change your if clause as below and remember to add exit() or die() function,this will end your php if there is no any data in database, and if there is any it will then start creating table for once and repeatedly fill up the table rows for given rows of data on database.
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
exit();
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
Move your echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
into the the if statement. This way it will only display the table when data is available!
$name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while ($row = mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>" . $name . "</td>
<td>" . $email . "</td>
<td>" . $desc . "</td>
</tr>";
}
echo "</tbody></table>";
}
Try this one. But don't forget to escape $_GET['name'] like htmlspecialchars and real_escape_string

Start tag em seen in table

I am coding php and seen this error while I trying to do a HTM5 validator. But the things is there is no in my whole file. Any suggestions what is wrong here?
Error Line 27, Column 4: Start tag em seen in table.
<em><br />
Edit note : I rarely want to add this code here due to the fact that's it is long and have nothing to do with the error (like I said above, there is no tag in the html)
<section>
<table>
<?php
include("./settings.php");
$conn = #mysqli_connect($host, $user, $pwd, $sql_db);
if (!$conn) {
echo "<p>Databse connection error</p>";
} else {
$eoinumber = trim($_POST["num"]);
$lname = trim($_POST["lname"]);
$query = "SELECT * FROM eoi WHERE EOInumber = $eoinumber AND lname = '$lname'";
$result = mysqli_query($conn,$query);
if (!$result) {
echo "<p>There is something wrong with the $query</p>";
} else {
$temp = mysqli_num_rows($result);
if ($temp == 0) {?>
<p>0 enquiry found. Please check again your EOInumber and last name</p>
<?php } else {
?>
<thead>
<tr>
<th>EOI number</th>
<th>Job ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Birth</th>
<th>Gender</th>
<th>Street</th>
<th>Town</th>
<th>State</th>
<th>Postcode</th>
<th>Mail</th>
<th>Telephone</th>
<th>Skills</th>
<th>Other Skills</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td>",$row["EOInumber"],"</td>";
echo "<td>",$row["job_num"],"</td>";
echo "<td>",$row["fname"],"</td>";
echo "<td>",$row["lname"],"</td>";
echo "<td>",$row["bday"],"</td>";
echo "<td>",$row["gender"],"</td>";
echo "<td>",$row["street"],"</td>";
echo "<td>",$row["town"],"</td>";
echo "<td>",$row["state"],"</td>";
echo "<td>",$row["postcode"],"</td>";
echo "<td>",$row["mail"],"</td>";
echo "<td>",$row["tele"],"</td>";
echo "<td>",$row["skill"],"</td>";
echo "<td>",$row["other"],"</td>";
echo "</tr>";
echo "</tbody>";
// echo "<p>Sucess</p>";
}
?>
</table>
Regardless of the presence or otherwise of the <em> tag, you are going to run into problems because you are opening the table tag and then potentially putting a <p> or other elements directly inside it, which is illegal (you can put <thead>, <tbody>, <caption>, <tr>, <tfoot> elements inside a <table>). It would be better if you opened the table after checking whether the queries had run successfully or not, i.e.:
<section>
<?php
include("./settings.php");
$conn = #mysqli_connect($host, $user, $pwd, $sql_db);
if (!$conn) {
echo "<p>Databse connection error</p>";
} else {
$eoinumber = trim($_POST["num"]);
$lname = trim($_POST["lname"]);
$query = "SELECT * FROM eoi WHERE EOInumber = $eoinumber AND lname = '$lname'";
$result = mysqli_query($conn,$query);
if (!$result) {
echo "<p>There is something wrong with the $query</p>";
} else {
$temp = mysqli_num_rows($result);
if ($temp == 0) {
echo "<p>0 enquiry found. Please check again your EOInumber and last name</p>";
} else {
echo "<table>";
... echo the table header...
while ($row = mysqli_fetch_assoc($result)) {
... echo table results...
}
echo "</table>";
}
}
?>
You missed a few }
So this should work:
<section>
<table>
<?php
include("./settings.php");
$conn = #mysqli_connect($host, $user, $pwd, $sql_db);
if (!$conn) {
echo "<p>Databse connection error</p>";
exit;
}
$eoinumber = trim($_POST["num"]);
$lname = trim($_POST["lname"]);
$query = "SELECT * FROM eoi WHERE EOInumber = $eoinumber AND lname = '$lname'";
$result = mysqli_query($conn,$query);
if (!$result) {
echo "<p>There is something wrong with the $query</p>";
exit;
} else {
$temp = mysqli_num_rows($result);
}
if ($temp == 0) { ?>
<p>0 enquiry found. Please check again your EOInumber and last name</p>
<?php } else { ?>
<thead>
<tr>
<th>EOI number</th>
<th>Job ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Birth</th>
<th>Gender</th>
<th>Street</th>
<th>Town</th>
<th>State</th>
<th>Postcode</th>
<th>Mail</th>
<th>Telephone</th>
<th>Skills</th>
<th>Other Skills</th>
</tr>
</thead>
<?php
while ($row = mysqli_fetch_assoc($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td>",$row["EOInumber"],"</td>";
echo "<td>",$row["job_num"],"</td>";
echo "<td>",$row["fname"],"</td>";
echo "<td>",$row["lname"],"</td>";
echo "<td>",$row["bday"],"</td>";
echo "<td>",$row["gender"],"</td>";
echo "<td>",$row["street"],"</td>";
echo "<td>",$row["town"],"</td>";
echo "<td>",$row["state"],"</td>";
echo "<td>",$row["postcode"],"</td>";
echo "<td>",$row["mail"],"</td>";
echo "<td>",$row["tele"],"</td>";
echo "<td>",$row["skill"],"</td>";
echo "<td>",$row["other"],"</td>";
echo "</tr>";
echo "</tbody>";
// echo "<p>Sucess</p>";
}
}
?>
</table>
<!--...-->

Categories