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
Related
As I'm new to PHP, I want to know that how to put data from different database tables into one table form on the page.
My codes so far as below,
<?php
include('DBconnect.php');
mysql_query("USE onlinerecruitment");
$username =$_SESSION['user'];
$result = mysql_query("SELECT * FROM application_data_file");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$pos_id = $rows['Position_ID'];
$resultt = mysql_query("SELECT * FROM position WHERE Position_ID = '".$pos_id."' ");
$resulttt = mysql_query("SELECT * FROM resume_data_file WHERE App_Email = '".$pos_id."' ");
?>
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
I will focus the part here.
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
But if there is any problem in the section that I didn't focused, I still appreciate your solution.
Thank you in advance.
To do this you would need to use a JOIN in the sql statement.
mysql_query("SELECT resume_data_file.App_Email, position.Position_ID FROM position INNER JOIN resume_data_file ON position.Position_ID = position.Position_ID WHERE position.Position_ID = '".$pos_id."' ");
http://www.w3schools.com/sql/sql_join.asp
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>
So far, I have this program that links my phpmyadmin database to my php script. Now, I need to display certain things in a table like "all records," "all contacts whose last name starts with S," "all pet owners," etc. My question is: is there a simpler way to insert code into my php script to display the information from my database. Right now I have that long echo statement to display the information. Is there a way I can just use something like a SELECT * statement to display all records and to simplify my code?
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli( $db_hostname,
$db_username,
$db_password,
$db_database);
if ($connection->connect_error) {
echo "Sorry";
} else {
echo "Connected!<br><br>";
$sql = "SELECT * FROM People";
$result = $connection->query($sql);
if (!$result) die ($connection->error);
$n = $result->num_rows;
for ($i=1; $i<=$n; $i++) {
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "<table>
<tr><th>ID</th><th>First Name</th><th>Last Name</th>
<th>Street Address</th><th>City</th>
<th>State</th><th>Zip Code</th>
<th>Email Address</th><th>Comment</th>
<th>Number of pets</th></tr>";
echo "<tr><td width=20>" . $row['iD'] . "</td><td>" . $row['First Name'] . "</td><td width=40>" .
$row['Last Name'] . "</td><td width=200>" . $row['Street Address'] . "</td><td width=30>" .
$row['City'] . "</td><td width=40>" . $row['State'] . "</td><td width=30>" .
$row['Zip Code'] . "</td><td width=40>" . $row['Email Address'] . "</td><td width=20>" .
$row['Comment'] . "</td><td width=10>" . $row['Number of pets'] . "</td></tr>";
}
echo "</table>";
}
?>
You should a table structure first then insert your PHP codes within the structure. E.g:
<?php
// Put your MYSQLI retrievals codes here
?>
<table>
<tr>
<th>FIELD_1</th>
<th>FIELD_2</th>
<th>FIELD_3</th>
</tr>
<?php
while ($rows = $result->fetch_array(MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $rows['field_1']; ?></td>
<td><?php echo $rows['field_2']; ?></td>
<td><?php echo $rows['field_3']; ?></td>
</tr>
<?php
}
?>
</table>
Technically you are doing everything alright, but there are some more sophisticated ways to develop the things you want.
Take a look at the following links:Object oriented programming in PHP and templating in PHP.
Hope this works for you:
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli($db_hostname,$db_username,$db_password,$db_database);
if ($connection->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
echo "Connected!<br><br>";
}
$last_name = "Lastname to search"; //You can post or get lastname in here.
/* create a prepared statement */
if ($sql = $connection->prepare("SELECT * FROM People WHERE `Last Name`=?")) {
/* bind parameters for markers */
$sql->bind_param("s", $last_name);
/* execute query */
$sql->execute();
/* instead of bind_result: */
$result = $sql->get_result();
/* close statement */
$sql->close();
}
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Email Address</th>
<th>Comment</th>
<th>Number of pets</th>
</tr>
</thead>
<tbody>
<?
/* now you can fetch the results into an array */
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td width=20><?=$row['iD'];?></td>
<td><?=$row['First Name'];?></td>
<td width=40><?=$row['First Name'];?></td>
<td width=200><?=$row['Street Address'];?></td>
<td width=30><?=$row['City'];?></td>
<td width=40><?=$row['State'];?></td>
<td width=30><?=$row['Zip Code'];?></td>
<td width=40><?=$row['Email Address'];?></td>
<td width=20><?=$row['Comment'];?></td>
<td width=10><?=$row['Number of pets'];?></td>
</tr>
<?
}
?>
</tbody>
</table>
I am trying to make something so it shows the database results. The problem is, it only shows 1 result. I want it to show multiple results using an quick and dirty template system. Also, is there a way to make my system better? Perhaps a class or a function? I need some insight on this. Thanks a bunch!
cp.php
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "********";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$activetournaments = "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
templates/cp.php
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</table>
I have changed $activetournaments to append to the end using '.='.
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td> ";
}
At the moment, for each record, it is overwriting $activetournaments. You could also use $activetournaments as an array then do a while / foreach in the template.
In your cp.php you overwrite $activetournaments in each while execution, change your code to:
<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "1ShotGG Tournaments | Control Panel";
require("templates/header.php");
if($mybb->user['uid'])
{
$uid = $mybb->user['uid'];
// Active Tournaments
// run queries, do all the brainwork
// lets get the forum name
$query = "SELECT tourneys.name, tourneys.date, tourneys.time
FROM tourneys
INNER JOIN players ON players.tid = tourneys.id
WHERE players.forumname = {$uid}";
$result = mysqli_query($conn, $query);
// output data of each row
$activetournaments = '';
while($row = mysqli_fetch_assoc($result)) {
$activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
// $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
// $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
}
}
else
{
$error = "Only registered members may access this area.";
include ('templates/error.php');
}
require ("templates/cp.php")
?>
In your templates/cp.php you can add a </tr> tag here:
<tr>
<?=$activetournaments?>
</tr>
your new templates/cp.php:
<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
<tr>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Playing as</th>
<th>Options</th>
</tr>
<tr>
<?=$activetournaments?>
</tr>
</table>
<hr />
<h3>Participated Tournaments</h3>
<table>
<tr>
<th>Position</th>
<th>Name</th>
<th>Date/time</th>
<th>Team</th>
<th>Played as</th>
<th>Options</th>
</tr>
<tr>
<?=$participatedtournaments?>
</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>
<!--...-->