Subsequent HTML not being displayed after PHP call - php

I am relatively new to PHP programming. I am building a small crud app (to-do list) to be exact for practice. I am working on fleshing out the UI and testing how information displays from my database on the page.
When a user logs in, they are shown a table of all the items they have saved.
table listing items
<div class="container">
<table class="table">
<tr>
<th>Completed</th>
<th>Description</th>
<th>Actions</th>
</tr>
<tr>
<td colspan="3"></td>
</tr>
<?php
$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
while($match_value = mysql_fetch_array($document_get)) {
?>
<tr>
<td>
Hi
</td>
</tr>
</table>
<?php
}
?>
</div>
php call to display list item
<?php
$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
while($match_value = mysql_fetch_array($document_get)) {
?>
Anything under this PHP request does not show up for some reason. I have looked at other posts where people have mentioned the same thing but I did not see anything specific that matched my issue per say.

Try to alter your query from:
$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
to
$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='".$user_id."' ORDER BY id DESC");

mysql_fetch_array returns an array. The structure of your while statement will not have the outcome that you desire. The while loop will continue to be true forever because nothing is changing in the while condition.
This should have the effect closer to what you are looking for:
$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
$match_value = mysql_fetch_array($document_get);
foreach ($match_value as $value){
//Do something
}
As a side note - I would highly recommend using PDO and not putting php in-line with html. Tutorial here: https://phpdelusions.net/pdo
If you use PDO your loop would look more like this:
while($row = $st->fetch(PDO::FETCH_ASSOC)){
//Do something
}

enter code here <?php
$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
while($match_value = mysql_fetch_array($document_get)) {
echo "
<tr>
<td>
Hi
</td>
</tr>
";
}
echo " </table>";
?>
Do the whole thing in php using echo to display the table elemts and Hi. Remove the extra php tags so loop and echo all php. Also look at security issues mentioned in comments. By the way, I moved the table end tag outside loop after reading comment below the question.

Related

php table <th> and <td> from Database Selected but they are not Matching in the right indexing position

all Developers.
I am developing School Management System, in the Database, I have two tables one is For Subjects, and the other one is designed for obtained marks of the Subjects and it is called scores.
So I am trying to fetch subject Names as Table Head
I have another Query below this query and I am trying to fetch from scores as table data .
At this point, I failed to match the subject name and its score from the scores table.
Here is my code:
<table class="table table-striped table-bordered">
<head>
<tr>
<?php
// Query for Subject Names
$view_subject = $config->prepare("SELECT * FROM subjects");
$view_subject->execute();
while($row = $view_subject->fetch()){
$sub_name = htmlspecialchars($row['sub_name']);
?>
<th class="text-center"style="background-color:#395C7F;color:#fff;"><?php echo $sub_name;?></th>
<?php } ?>
</tr>
</thead>
<body>
<?php
// Query for Subject Scores
$view_scores = $config->prepare("SELECT * FROM scores INNER JOIN subjects ON scores.score_sub_id = subjects.sub_id WHERE scores.std_rand = :random_id ORDER BY scores.score_sub_id ASC");
$view_scores->execute(['random_id' => $rand_ID]);
while($row = $view_scores->fetch()){
$score_id = htmlspecialchars($row['score_id']);
$score_sub_id = htmlspecialchars($row['score_sub_id']);
$score_mid_amount = htmlspecialchars($row['score_mid_amount']);
$score_final_amount = htmlspecialchars($row['score_final_amount']);
?>
<tr>
<td class="text-black" data-title="Subject"><?php echo $score_mid_amount;?></td>
</tr>
<?php } ?>
</tbody>
</table>
Database images:
1- Subjects table
2- Scores table
** Browser UI **
On your second loop you have entered '<tr>' wrapping each '<td>' that means that each one arrives at a different line , '<td>'s should be as much as there are '<th>' for each line.... so :
<?php
// Query for Subject Scores
$view_scores = $config->prepare("SELECT * FROM scores INNER JOIN subjects ON scores.score_sub_id = subjects.sub_id WHERE scores.std_rand = :random_id ORDER BY scores.score_sub_id ASC");
$view_scores->execute(['random_id' => $rand_ID]);
?>
<tr>
<?php
while($row = $view_scores->fetch()){
$score_id = htmlspecialchars($row['score_id']);
$score_sub_id = htmlspecialchars($row['score_sub_id']);
$score_mid_amount = htmlspecialchars($row['score_mid_amount']);
$score_final_amount = htmlspecialchars($row['score_final_amount']);
?>
<td class="text-black" data-title="Subject"><?php echo $score_mid_amount;?></td>
<?php } ?>
</tr>
</tbody>
</table>
This should fix your table but it will only create one line! if you have more than one line you will need to add another loop to wrap this one and it will create the new '<tr>' outside the inner loop.
BTW: I assume that the 2nd while loop is exactly long as the first one... since you are supposed to have the same amount of <td> per line per <th> if it's not in the same length or not sorted the same way you will have an issue... which can be resolved either by adjusting your SELECT or creating an array with ids and injecting to it the data from the second loop according to the keys brought in the first.

How do I add another mysqli query into a while loop?

So I am carrying out a query and returning the results in the form of a table. The code below works well.
<table>
<thead>
<tr style="text-align: center;">
<th>Activity</th>
<th>Description</th>
<th>Frequency</th>
<th>Mandatory</th>
<th>Added Yet</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $conn->prepare("SELECT * FROM knowledgebase ORDER BY category ASC");
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) echo "<tr><td>No activities found</td><td></td><td></td><td></td><td></td><td></td></tr> </tbody>
</table></br></br>
Why not add your first activity from our Knowledge base or create a new activity of your own";
else {
while($row = mysqli_fetch_array($result)) {
$activity_id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$frequency = $row['frequency'];
$mandatory = $row['mandatory'];
echo "<tr><td>".$title."</td><td>".$description."</td><td style=\"text-align: center;\">".$frequency."</td><td style=\"text-align: center;\">".$mandatory."</td><td></td></tr>";
}
}
$stmt->close();
?>
</tbody>
</table>
What I want to add in is another query inside the final <td></td>. What I want to do is query a second db table and say if this activity has already been added to the users table, echo YES, otherwise, echo NO.
The problems is, adding the query into the while loop keeps throwing errors.
Any suggestions gratefully received.
Instead of running another query for every row, you could join the users table to the knowledgebase table in your first query. If you use a left join, you'll still get all the rows from knowledgebase.
SELECT knowledgebase.*, userstable.activity_id
FROM knowledgebase
LEFT JOIN userstable ON knowledgebase.id = userstable.activity_id
ORDER BY category ASC
Then in your last <td>, you can print YES/NO depending on whether or not there was a matching row in the users table.
...<td><?php echo $row['activity_id'] ? 'YES' : 'NO' ?></td>...
(I made up names for your other table and column, but I think it shows the general idea.)

SQL Server ORDER BY Not working in PHP

$query=mssql_query ('SELECT USER_INDEX_ID FROM T_o2jam_login');
echo "<table border =\"0\" style=\"color: gray;\" cellspacing=\"0\" cellpadding=\"0\" CLASS='boldtable'><tr><th colspan=\"9\">Online Players</th></tr><tr><td>Level </td> <td> Nick </td> </tr>";
if (mssql_num_rows($query)) {
while ($row = mssql_fetch_array($query)) {
$q2 = mssql_query ("select * from t_o2jam_charinfo where USER_INDEX_ID=$row[USER_INDEX_ID] ORDER BY Level DESC");
$nt=mssql_fetch_array($q2);
echo "<tr><td>Lv. $nt[Level] </td><td> $nt[USER_NICKNAME] </td></tr>" ;
I am trying to sort online users by Level on Descending order. From 99 to Level 1. It displays the data but they are not sorted. What's the problem right there? Thank you!
You need to have the order by in your first query. Your second query is within a loop, so it's going to first go by the order of the first one.
This really should be one query with a JOIN.
SELECT charinfo.*, FROM t_o2jam_charinfo charinfo
INNER JOIN T_o2jam_login login
ON charinfo.USER_INDEX_ID = login.USER_INDEX_ID
ORDER BY `Level` DESC
But since you never use any of the info from the "login" table, one wonders why it's even used at all.
Anyway, never run queries in loops.

Preventing dynamic PHP table display correctly

I have the following code to display a block of six products from a mysql database.
It displays as two columns three rows and I get individual photos and correct page links in each of the six positions but the alt tags for the three products in column one are repeated in column 2. I cannot work out why. Any thoughts, and ways to improve the code?
<?php
// create query
$query = "SELECT * FROM photogear WHERE qty != 0 ORDER BY id DESC LIMIT 6";
// execute query
$result = mysql_query($query) or die(MYSQL_ERROR);
?>
<table>
<?php
while($row = mysql_fetch_array($result)){
$product2=$row['product'];
$img2=$row['img'];
$manuid2=$row['manuid'];
$id2=$row['id'];
$price=$row['price'];
//GET MANUFACTURER FOR DISPLAY IN img title
$manu_q = "SELECT * FROM manufacturers WHERE manuid = '$manuid2' ORDER BY name";
$manu_r = mysql_query($manu_q) or die(mysql_error());
$manu_info = mysql_fetch_array($manu_r);
$name2=$manu_info['name'];
?>
<tr>
<td>
<?php // for each product show photo with a link to product page
echo "<a href='product-".$row['id']."'><img src='".$row['img']."'alt='$name2,$product2 $price' title='$name2 $product2 £$price' width='85'></a>";
?>
<?php $row=mysql_fetch_assoc($result); // make one record out.?>
</td>
<td>
<?php // for each product show photo with a link to product page
echo "<a href='product-".$row['id']."'><img src='".$row['img']."' alt='$name2, $product2 $price' title='$name2 $product2 £$price' width='85'></a>";?>
</td>
</tr>
<?php
} // End loops.
?>
</table>
Any help much appreciated
Instead of using $product2, $price try using $row['product'],$row['price'] in alt
Just try this but im not sure
Use this
SELECT * FROM manufacturers WHERE manuid = '$row['manuid']' ORDER BY name
Instead of this
SELECT * FROM manufacturers WHERE manuid = '$manuid2' ORDER BY name
After this
$row=mysql_fetch_assoc($result);
Add this line
$manu_info = mysql_fetch_assoc($manu_r);

PHP/MySQL where/order clause not ordering data

I am trying to order data from the table 'tech_inquiry' by the Field 'number' in descending order. This should put the results in order by year. Each 'number' field corresponds to another field with a title/date (what is actually viewed by visitors) which I can't sort by because the date is at the end of the title and not always in the same place.
Also, the table 'tech_inquiry_allowed' determines what is viewable to who when logged in.
With that said, here is the code:
<?
$query2=mysql_query("SELECT * FROM tech_inquiry_allowed where term_code = '$term_code' ");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$query3=mysql_query("SELECT * FROM tech_inquiry WHERE id= '$id2' ORDER BY number DESC");
$row3=mysql_fetch_assoc($query3);
$name3=$row3['name'];
?>
<hr />
<li><? echo $name3; ?> </li>
<?
}
?>
Also, I have another 'admin' section that is able to order data correctly. The only difference is there is no conditional 'where' clause because no user authentication is needed (it is used to add data to the table).
Here is that code:
<?
$query2= mysql_query("SELECT * from tech_inquiry ORDER BY number DESC");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$name2=$row2['name'];
?>
<hr />
<li><? echo $name2; ?> </li>
<?
I am wondering if it might be the fact that we are running a query inside a loop.
There are a number of problems here. First of all you only need one query to accomplish this. Please read up on SQL query writing when you get a moment. You will find it to be VERY helpful.
Second, you are using way more code than you need to. Below is the much simplified, cleaner, and probably faster code.
<?php
$query = mysql_query("SELECT * FROM tech_inquiry ti WHERE id IN (SELECT id FROM tech_inquiry_allowed where term_code = '$term_code') ORDER BY ti.number");
while ($row = mysql_fetch_object($query)) {
echo "<hr />\n<li><a href='get_file.php?id={$row->id}'>{$row->name}</a></li>";
}
?>
You are not looping the inner query. Anyway, you should be using a single query for this:
SELECT allowed.*, inquiry.*
FROM tech_inquiry_allowed allowed
INNER JOIN tech_inquiry inquiry
ON inquiry.id = allowed.id
WHERE term_code = '$term_code'
ORDER BY inquiry.number DESC

Categories