Query data from multiple database tables into form table (MYSQL,PHP) - php

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

Related

HTML button not selecting correct form value

HTML button not selecting correct form value.
The following code selects data and displays it correctly in a html table: fname, lname , customer_id and the last column in the table shows a button labelled more info.
The first part of the code works fine.
<table>
<thead>
<tr>
<th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$sql = "select fname, lname,customer_id from customer_address ";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$fname = $row['fname'];
$lname = $row['lname'];
$customer_id = $row['customer_id'];
echo" <tr>";
echo"<td>$fname</td>";
echo"<td>$lname</td>";
echo"<td>$customer_id</td>";
echo"<td><input type='hidden' name='customer_id' value='$customer_id'>";
echo "<button type='submit' name='submit'>More Info</button></td>" ;
}
echo" </tr>";
?>
</tbody>
</table>
</form>
With the second part of the code, I want to grab the customer_id when the button is clicked and display it below the table(I am going to uses the variable in a subsequent query to display more information later). The button selects the same customer_id no matter which button is clicked, and it is always the last record in the table. I would appreciate any help in correcting my error.
<?php
$ccf=" ";
if(isset($_POST['submit'])){
$ccf = $_POST['customer_id'];
}
echo "Customer Id selected is:$ccf";
?>
Your elements are all inside one form, so there are multiple elements with name='customer_id'. Place each row inside their own separate <form></form>:
<table>
<thead>
<tr>
<th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
</tr>
</thead>
<tbody>
<?php
$sql = "select fname, lname,customer_id from customer_address";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$fname = $row['fname'];
$lname = $row['lname'];
$customer_id = $row['customer_id'];
echo" <tr>";
echo "<td>$fname</td>";
echo "<td>$lname</td>";
echo "<td>$customer_id</td>";
echo "<td>";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='customer_id' value='$customer_id'>";
echo "<button type='submit' name='submit'>More Info</button>";
echo "</form>";
echo "</td>";
echo" </tr>";
}
?>
</tbody>
</table>

Search Results display

i have a website project, where customers will need to search and the results will display, so i have done all that, but the problem is when a search results comes up,if there's more than 1 row,it dublicate the results on the same row,here are the code.
<h1 style="font-family:calibri;color:#13CC0D;text-align:center;">BODABODA SEARCH RESULTS</h1>
<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
<tr>
<th style="border-left: 1px solid #C1DAD7"> Region</th>
<th> District </th>
<th> Ward </th>
<th> Street</th>
<th> Driver Name </th>
<th>Identification Type</th>
<th>Identification Number</th>
<th>Motorcycle Type</th>
<th>Motorcycle Reg No</th>
<th>Phone Number</th>
</tr>
</thead>
<?php
$conn = mysqli_connect('localhost', 'efalococ_calcia', '*ad4#zNQ=nfN') or die('can not connect to the server'.mysqli_error);
mysqli_select_db($conn, 'efalococ_safirii');
if(isset($_POST['search'])){
$query = $_POST['region'];
$query1 = $_POST['district'];
$query2 = $_POST['ward'];
$query3 = $_POST['street'];
$results = mysqli_query($conn,"SELECT * FROM bodaboda WHERE (`region` LIKE '%".$query."%') && (`district` LIKE '%".$query1."%') && (`ward` LIKE '%".$query2."%') && (`street` LIKE '%".$query3."%')") or die(mysql_error());
if(mysqli_num_rows($results) >0){
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
}
}else{
echo '<span style="color:red;font-family:cursive;font-size:14px;">No results found, Please Modify your search </span> Click here'.mysqli_error($conn);
}
}
?>
</table>
And the results shows like this!
Can you please help on that? any idea?
You just need to use the <tr> for every row.
The tag defines a row in an HTML table.
Learn more about HTML tr tag
while($row = mysqli_fetch_array($results)) {
echo "<tr>";
echo "<td>".$row['Region']."</td>" ;
echo "<td>".$row['District']."</td>";
echo "<td>".$row['Ward']."</td>";
echo "<td>".$row['Street']."</td>";
echo "<td>".$row['DriverName']."</td>";
echo "<td>".$row['IdentificationType']."</td>";
echo "<td>".$row['IdentificationNumber']."</td>";
echo "<td>".$row['MotorcycleType']."</td>";
echo "<td>".$row['MotorcycleRegNo']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "</tr>";
}

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>

How can get the value of the selected cell from a table created by php?

After I printed those values by using PHP, I got confused how to get the value of a selected cell.
<div>
<php
$query = mysql_query(select * from items, $conn);
echo "<table border='1'>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>";
while(row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
}
echo "</table>";
?>
</div>
seems you doing fine, iv'e just edited some typos, added tags,and changed to: "mysql_fetch_assoc" function. try this:
<div>
<?php
$query = mysql_query("select * from items", $conn);
echo "<table border='1'>
<tr>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>
</tr>";
while($row = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>

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

Categories