How to display rows with same id but different row values? - php

I'm not sure how to describe it in words so i'll show you:
View scores table
I'm trying to display rows that has the same id, and display their values but in different tables. As seen in the picture, I have to make a table like that for every row that has the same ID. The main purpose of it is to compare their scores from before, when they first took the exams, to what the score they got now from the same set of exams.
This is what my database looks like right now. They have different scoreIDs and other row values but has the same id_applicant for it refers that these scores are of this certain applicant. Database Snippet
This is what my code for it looks like. (id_applicant is the one that they will have the same id)
<!-- VIEW SCORES -->
<div style="background-color: #E9E9E9" class="tab-pane" id="ViewScoresTab">
<div class="list-group">
<div id="view-scores">
<div class="form-group" style="margin-left:20px">
<br>
<div id="inputhere"></div>
<h1><span class="label label-default">View Scores</span></h1>
<br><br>
<table>
<tr>
<th bgcolor= '#00BFFF'>TEST</th>
<th bgcolor= '#FFD700'>SCORES</th>
<th bgcolor= '00FF00'>REMARKS</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "offsourcing";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($ids_holder)){
$q = "SELECT * FROM scores WHERE id_applicant = $ids_holder";
$result = mysqli_query($conn, $q);
$rowcount = mysqli_num_rows($result);
for(int x=0;x!=rowcount;x++){
//some code for here
}
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo
"<tr>
<td class= 'test'> Grammar </td>
<td> $row[2] </td>
<td> $row[8] </td>
</tr>
<tr>
<td class= 'test'> Customer Service </td>
<td> $row[3] </td>
<td> $row[9] </td>
<tr>
<td class= 'test'> Logical Reasoning </td>
<td> $row[4] </td>
<td> $row[10] </td>
</tr>
<tr>
<td class= 'test'> Idioms </td>
<td> $row[5] </td>
<td> $row[11] </td>
</tr>
<tr>
<td class= 'test'> Call Listening (1) </td>
<td> $row[6] </td>
<td> $row[12] </td>
</tr>
<tr>
<td class= 'test'> Call Listening (2) </td>
<td> $row[7] </td>
<td> $row[13] </td>
</tr>";
mysqli_close($conn);
}
}
?>
</table>
<br><br>
<br><br>
<br><br>
</div>
</div>
</div>
</div>
</body>
</html>
I'm not exactly sure how to do it yet so any idea?

Instead of targeting the number in the Array, directly target the column.
So replacing your while loop with something like:
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo
'<tr>
<td class= 'test'> Grammar </td>
<td>'.$row['GrammarScore'].'</td>
</tr>
<tr>
<td class= 'test'> Customer Service </td>
<td>'.$row['CustomerServiceScore'].'</td>
</tr>'; //etc }
Is this what you mean? You said something about comparing the older scores to the newer ones, but you didn't really post anything about how you're calling these records, assuming they're on a different table?

Related

SQL query fails in prod environment on IIS but works in test environment running Apache

I'm having a bit of an issue with quires failing when I upload to the live environment while they are fully functional in the test environment, I'm at a loss at this point about where the error may be so I finally broke down and decided to ask those of stack overflow that may be much more skilled than myself if there's something I've missed. The quires look at serial number information from two different tables to pull system specs and any hard drive information associated with the parent serial
<?php
$Dev_HDD = 0;
$con=mysqli_connect("localhost","username","user_password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST["serial"]))
{
$SerialNumber = $_POST["serial"];
$sql ="SELECT system.Manufacturer, system.Model, system.SerialNumber, system.Processor, system.Memory FROM system WHERE SerialNumber = '" .$SerialNumber. "'" ;
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$System_Manufacturer = $row[0];
$System_Model = $row[1];
$System_SerialNumber = $row[2];
$System_Processor = $row[3];
$System_Memory = $row[4];
?>
<table border="0" height="100%" width="100%" align="left" valign="top">
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2" valign="top">System Summary:</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td>System Manufacturer</td>
<td>
<?php echo $System_Manufacturer; ?>
</td>
</tr>
<tr>
<td>System Model:</td>
<td>
<?php echo $System_Model; ?>
</td>
</tr>
<tr>
<td valign="top">System Serial Number</td>
<td valign="top">
<?php echo $System_SerialNumber; ?>
</td>
</tr>
<tr>
<td valign="top">System Processor</td>
<td valign="top">
<?php echo $System_Processor; ?>
</td>
</tr>
<tr>
<td valign="top">System Memory</td>
<td valign="top">
<?php echo $System_Memory; ?>
</td>
</tr>
<?php
}
// Free result set
mysqli_free_result($result);
}
else
{
echo "The serial number specified could not be located<br>";
}
$sql ="SELECT device.recid, device.Manufacturer, device.Model, device.SerialNumber, device.Capacity, device.RPM, device.ErasureMethod, device.ErasureResults FROM device WHERE SystemSerialNumber = '" . $System_SerialNumber . "'" ;
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$Dev_Recid = $row[0];
$Dev_Manufacturer = $row[1];
$Dev_Model = $row[2];
$Dev_DeviceSerialNumber = $row[3];
$Dev_Capacity = $row[4];
$Dev_RPM = $row[5];
$Dev_ErasureMethod = $row[6];
$Dev_ErasureResults = $row[7];
?>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2" valign="top">Storage Summary(<?php echo $Dev_HDD = $Dev_HDD + 1; ?>):</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td>Hard Drive Manufacturer</td>
<td>
<?php echo $Dev_Manufacturer; ?>
</td>
</tr>
<tr>
<td>Hard Drive Model:</td>
<td>
<?php echo $Dev_Model; ?>
</td>
</tr>
<tr>
<td valign="top">Serial Number Lookup</td>
<td valign="top">
<?php echo $Dev_DeviceSerialNumber; ?>
</td>
</tr>
<tr>
<td valign="top">Hard Drive Capacity</td>
<td valign="top">
<?php echo $Dev_Capacity; ?>
</td>
</tr>
<tr>
<td valign="top">Hard Drive Speed</td>
<td valign="top">
<?php echo $Dev_RPM; ?>
</td>
</tr>
<tr>
<td>Erasure Method:</td>
<td>
<?php echo $Dev_ErasureMethod; ?>
</td>
</tr>
<tr>
<td>Erasure Results:</td>
<td>
<?php echo $Dev_ErasureResults; ?>
</td>
</tr>
<tr>
<td>Parent Serial Number:</td>
<td>
<?php echo "<a href='logs/" .$Dev_DeviceSerialNumber.".log' target='_blank'>".$Dev_DeviceSerialNumber."</a> <br>";?>
</td>
</tr>
</table>
<?php
}
}
// Free result set
mysqli_free_result($result);
mysqli_close($con);
}
?>
<?php
echo " <br>";
echo "<pre></pre>";
?>

How to update the database. After adding data from the database to the form

How to update the database. I will not add new records.
The data is identical to the code retrieved from the database. I do not know how
to embed the second function, from the form and update the database.
reading from the database to the form works
<form action="" method="put">
<table width="100%">
<thead>
<tr>
<td class="colsp" colspan="5">Terminarz</td>
</tr>
<tr class="subcolor title-row">
<td style="width: 10%">id</td>
<td style="width: 40%" class="left">Gospodarz</td>
<td style="width: 40%">Gość</td>
<td style="width: 5%">Home<br/>Goals</td>
<td style="width: 5%">Guest<br/>Goals</td>
</tr>
</thead>
<tbody>
<?php
if(isset($_POST['Submit'])){
$hGoals = $_POST['inphomegoals'];
$gGoals = $_POST['inpguestGoals'];
$update="UPDATE matches SET homeGoals=$hGoals, guestGoals=$gGoals where matchID = ".$matchID;
$stmt = $conn->prepare($update);
$stmt->execute();
echo $stmt->rowCount() . " records UPDATED successfully";
}
$display = $conn->prepare(
"SELECT
m.matchID,
hc.clubName as homeClub,
gc.clubName as guestClub,
m.homeGoals,
m.guestGoals
FROM matches AS m
JOIN clubs AS hc ON hc.clubID = m.homeID
JOIN clubs AS gc ON gc.clubID = m.guestID
ORDER BY matchID ASC ");
$display->execute();
$results = $display->fetchAll();
foreach ($results as $index => $row){
?>
<tr>
<td><label><?php echo $row['matchID']; ?></label></td>
<td><label><?php echo $row['homeClub']; ?></label></td>
<td><label><?php echo $row['guestClub']; ?></label></td>
<td><label><input type="number" name="inphomegoals" value="<?php echo $row['homeGoals']; ?>"/> : </label></td>
<td><label><input type="number" name="inpguestGoals" value="<?php echo $row['guestGoals']; ?>"</label></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td colspan="5"><input type="submit" value="Add To Base" name="Submit" style="width: 150px; height: 45px"</td>
</tr>
</tfoot>
</table>
</form>
connect.php
<?php
$db_server="localhost";
$db_username="root";
$db_password="";$db_database="football_db";
$conn=new
PDO("mysql:host=$db_server;dbname=$db_database",$db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
you can create a new button like add, name it update.
Redirect it to the the new page for better handling of code if your code is going to get bigger.
on that page do the same thing like you did above, just change your query from SELECT to UPDATE.
This is the simplest way you can do, You can avoid DRY code by using the same form and use different query for update.

Display image PHP MYSQLI use WHERE condition

I have a program that insert many records and image to a database. I must display my records and image into the web page (forms-output.php) but only 4 records and images. If I click the link (example: link in the ref_code) and records and image must display into a new web page (forms-full-data.php) but only records and image when I click what to display. Almost all work has been completed, but when I click a field in the ref_code all records have been successfully displayed except the images. Can you help me please?
This is forms-output.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "profil";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo '
<div class="table-responsive">
<table class="table table-bordered table-striped mb-none" id="datatable-tabletools" data-swf-path="assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf">
<thead>
<tr align="center" bgcolor="#E9E9E9">
<td align="center" width="1"><b>No</td>
<td width="10%" align="center"><b>Photo</td>
<td width="100" align="center"><b>Ref Code</td>
<td width="100" align="center"><b>Name</td>
<td width="100" align="center"><b>Date</td>
</tr>
<tr>';
$no = 1; //inisialisasi untuk penomoran data
$sql= "SELECT image, name, ref_code, image, overseas, avaibility, sector, country, date, height, weight, religion, status, children, education, language, language2, experience1, experience2, experience3, experience4, experience5, other1, other2, other3, other4, other5, other6, working_experience, working_experience2, working_experience3, working_experience4, working_experience5 FROM tb_profil";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$gambar="<img src='images/" . $row["image"] . "' width='200'" . "' height='150' ></td><td align='center'>";
$data="<b>$row[name]<br><br>$row[ref_code]<br><br>$row[overseas]<br><br>$row[avaibility]<br><br>$row[sector]<br><br>$row[country]<br><br>$row[date]<br><br>$row[height]<br><br>$row[weight]<br><br>$row[religion]<br><br>$row[status]<br><br>$row[children]<br><br>$row[education]<br><br>$row[language]<br><br>$row[language2]<br><br>$row[experience1]<br><br>$row[experience2]<br><br>$row[experience3]<br><br>$row[experience4]<br><br>$row[experience5]<br><br>$row[other1]<br><br>$row[other2]<br><br>$row[other3]<br><br>$row[other4]<br><br>$row[other5]<br><br>$row[other6]<br><br>$row[working_experience]<br><br>$row[working_experience2]<br><br>$row[working_experience3]<br><br>$row[working_experience4]<br><br>$row[working_experience5]$row[image]";
echo "<tr><td align='center'>" .$no. "</td><td>$gambar<a href='forms-full-data.php?p=$data'>" . $row["ref_code"] . "</td><td align='center'>" . $row["name"] . "</td><td align='center'>" . $row["date"] . "</td></tr>";
$no++;
}
echo "</table>";
} else {
echo "0 results";
}
?>
This is forms-full-data.php:
<?php
// server info
$host = 'localhost';
$user = 'root';
$password = '';
$database_name = 'profil';
$mysqli = new mysqli($host, $user, $password, $database_name);
$res = $mysqli->query("SELECT * FROM tb_profil WHERE ref_code='" . $_GET['p']."'");
$row = $res->fetch_assoc();
$gambar="<img src='images/" . $row["image"] . "' width='150'" . "' height='200' >";
if (isset($_GET['p'])) {
echo "<table width='100%' border='0'>
<tr>
<td rowspan='31' width='20'>
<td width='180'>Full Name</td>
<td rowspan='15' width='230'> </td>
<td rowspan='31' width='200'>$_GET[p]</td>
<td rowspan='5' width='200'>$gambar</td>
</tr>
<tr>
<td>Ref Code</td>
</tr>
<tr>
<td>Overseas Experience</td>
</tr>
<tr>
<td>Avaiability</td>
</tr>
<tr>
<td>Sector</td>
</tr>
<tr>
<td>Country</td>
</tr>
<tr>
<td>Date Of Birth</td>
</tr>
<tr>
<td>Height</td>
<td></td>
</tr>
<tr>
<td>Weight</td>
<td rowspan='17'> </td>
</tr>
<tr>
<td>Religion</td>
</tr>
<tr>
<td>Marital Status</td>
</tr>
<tr>
<td>Children</td>
</tr>
<tr>
<td>Education</td>
</tr>
<tr>
<td>Language</td>
</tr>
<tr>
<td>More Language</td>
</tr>
<tr>
<td rowspan='5'>Experience</td>
<td width='10'>Care For Children</td>
</tr>
<tr>
<td>Cooking</td>
</tr>
<tr>
<td>Care For Infant</td>
</tr>
<tr>
<td>Care For Newborn</td>
</tr>
<tr>
<td>Care For Elderly</td>
</tr>
<tr>
<td rowspan='6'>Other Information</td>
<td>Able To Handle Pork?</td>
</tr>
<tr>
<td>Able To eat Pork?</td>
</tr>
<tr>
<td>Able to care for dog/cat?</td>
</tr>
<tr>
<td>Able to swim?</td>
</tr>
<tr>
<td>Willing to lock after elderly forbidden?</td>
</tr>
<tr>
<td>Willing to work with no off days?</td>
</tr>
<tr>
<td>Working Experience</td>
<td rowspan='5'></td>
</tr>
<tr>
<td>More Working Experience 2</td>
</tr>
<tr>
<td>More Working Experience 3</td>
</tr>
<tr>
<td>More Working Experience 4</td>
</tr>
<tr>
<td>More Working Experience 5</td>
</tr>
</table>";
}
?>
Add '/', before 'images' catalog, while You build path.
Please read about 'SQL injection".
Please use ADODB to manage DB.

Unable To View All Entries In Table

I wonder whether someone may be able to help me please.
Firstly my apologies. I would have liked to provide a link to this rather than printing the code below, but I need this to run off my live server tables.
I'm using the code below to generate a table of all location records pertinent to the current user.
<form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
<table width="864" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="17"></th>
<th width="99"><div align="center">Location Name</div></th>
<th width="287"><div align="left">Location Address</div></th>
<th width="88"><div align="center">No. Of Finds Made </div></th>
<th width="86"></th>
<th width="72"></th>
<th width="84"></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT l.locationid, f.locationid, l.locationname, l.userid, l.returnedaddress, count(f.locationid) as totalfinds FROM detectinglocations as l left join finds as f on l.locationid=f.locationid WHERE l.userid='$idnum' ORDER BY l.locationname";
$result = mysql_query($query) or die('error');
if (mysql_num_rows($result) == 0)
echo"<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan='6'><div align='center'><strong>There are no records set up</strong></div></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
else
{
while($obj=mysql_fetch_object($result))
{
?>
<tr>
<td><input type="hidden" name="lid" value="<?php echo $obj->locationid;?>"/></td>
<td><div align="center"><?php echo $obj->locationname;?></div></td>
<td><div align="left"><?php echo $obj->returnedaddress;?></div></td>
<td><div align="center"><?php echo $obj->totalfinds;?></div></td>
<td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Images"/></div></td>
<td width="129"><div align="left"><input name="type" type="submit" value="View Location Finds"/></div></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Although the query is retrieving the right information and the buttons on the row work, the problem I'm having is that although there should 3 records shown in the list, only the first is shown.
I'm the first to admit that I'm certainly no expert when it comes to PHP, but I've been working on this for days and written the script many, many times, but I just can't seem to find a solution.
I just wondered whether someone could possibly look at this please and let me know where I'm going wrong.
As pointed out by both #Bulk and #ShawnVaser there was a problem with my query. This is the solution:
Query
$query = "SELECT l.*, COUNT(f.locationid) as totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '28' GROUP BY l.locationname";
$result = mysql_query($query);

:ERR_CONNECTION_CLOSED connection closed unexpectedly

I get that error in php..And I have no idea why...
Here is how my connection is established:
<?php
$hostname_QASite = "localhost";
$database_QASite = "qasite";
$username_QASite = "root";
$password_QASite = "";
$QASite = mysql_pconnect($hostname_QASite, $username_QASite, $password_QASite) or trigger_error(mysql_error(),E_USER_ERROR);
?>
my query is the following:
mysql_select_db($database_QASite, $QASite) or die(mysql_error());
$query_get_all_topics = "SELECT topic_id, title FROM topic";
$get_all_topics = mysql_query($query_get_all_topics, $QASite) or die(mysql_error());
$row_get_all_topics = mysql_fetch_assoc($get_all_topics);
$totalRows_get_all_topics = mysql_num_rows($get_all_topics);
And then I iterate over the row_get_all_topics ...
What is wrong in the code ?
Edit:
I get that error, when I try to loop 2 times over different results in the database.
UPDATE:
<body>
<br/><br/><br/><br/><br/><br/><br/>
<div align="center">
<ul id="navlist">
<li> צור נושא</li>
<li> ראה קשרים</li>
</ul>
<?php do { ?>
<table border="1">
<tr>
<td>
<table width="100%" border="1" >
<tr>
<td width="90%" align="right">
<?php echo $row_get_all_topics['title']; ?>
</td>
<td width="10%">
:שם נושא
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<?php
//$result=$mysql_query("SELECT title, sub_topic_id FROM sub_topic WHERE topic_id=".$row_get_all_topics['topic_id']) or die(mysql_error());
$result="";
if($row=mysql_fetch_array($result))
{
do
{
?>
<table >
<tr>
<td>
<?php echo $row['title']; ?>
</td>
<td>
:תת נושא
</td>
</tr>
<tr>
<td colspan="2">
<table>
<tr>
<td>
</td>
</tr>
<tr>
<td>
עדכן
</td>
<td>
מחק
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</td>
</tr>
</table>
<?php
**}while($row=mysql_fetch_assoc($result));** 1 FIRST LOOP
}//end suptopic search
?>
<?php **} while ($row_get_all_topics = mysql_fetch_assoc($get_all_topics)); ?>** 2ND LOOP
AS soon as I add this line, to query teh database inside the loop, the page shows the error..
$result=$mysql_query("SELECT title, sub_topic_id FROM sub_topic WHERE topic_id=".$row_get_all_topics['topic_id']) or die(mysql_error());

Categories