PHP outputs the wrongly formatted html table - php

I'm having trouble echoing data into a HTML table.
It comes out like that:
But it should be:
Here's the code. What am I doing wrong?
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</table>";
}
}
else{
echo "No results";
}
}
?>

The problem is that you keep outputting a new table for each iteration.
Your code should look like this:
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table>"; // Start the table
// Output the table headers
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</tr>";
}
echo "</table>"; // End the table
}
else{
echo "No results";
}
}
?>

just put echo "<table>"; and first tr creation statment out side of while loop and also put closing of table after finishing of while loop and see i am sure it will work.
Try
<?php
echo "<table><tr><td>Model name</td><td>Year</td></tr>";
while($results = mysql_fetch_array($raw_results))
{
echo "<tr><td>".$results['mudeli_nimetus']."</td><td>".$results['soetusaasta']."</td></tr>";
}
echo "</table>";
?>

use this code, you have to first start the table, use while loop to iterate the result, and then close the table.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";

Remove table code from while loop and put outside.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";

replace your if block, use the following code , hope it may help you
if(mysql_num_rows($raw_results) > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}echo "</table>";
}

Related

deleting a row with a particular id in while loop

<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
$_SESSION['delete'] = $row['movieid'];
echo "<td>";
echo"</tr>";
}
echo "</table>";
?>
if (isset($_POST['deletem'])) {
$delete=$_SESSION['delete'];
$query6 = "DELETE FROM movieinfo WHERE movieid=$delete";
mysqli_query($db, $query6);
}
i need to delete the particular row with the id selected so i am storing in session the id and deleting it but when i press on the deleting button it deletes the last row only its because session is storing all the id's because of loop how should i fix it?
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
**echo "</td>";
$mid = isset($_GET['mid']) ? $_GET['mid'] : '';
$del = "DELETE FROM movieinfo WHERE movieid=$mid";
mysqli_query($db, $del);
{
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
}
echo"</tr>";
}
echo "</table>";
?>

php results 2 table on page instead of one with all data

I've made a script for a search mysql table, and instead of one table with more rows, he return me on results page one table for each person.
This is my results code by id from the search script. and returns table for each person
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo"</thead>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
echo"</table>";
echo "</center>\n";}
You need to write your table and table head html outside your while loop Add mysql_num_rows to check empty result and add mysql_real_escape_string to Escapes special characters in a string for use in an SQL statement
$id = $_GET['id'];
$id=mysql_real_escape_string($id);
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
$row=mysql_num_rows($rst);
if($row>0){
//outside while loop
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th>";
echo"</thead>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr><td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
}
//outside while loop
echo"</table>";
echo "</center>\n";
}
Note:- Don't use mysql because it is deprecated instead use mysqli or
PDO
To prevent sql injection check this link How can I prevent SQL-injection in PHP?
You have to get table part outside from while loop.
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo "</thead><tbody>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>";
}
echo"</tbody></table>";
echo "</center>\n";

Displaying Multiple Rows with two tables

I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.
Hope someone could give some advice with my line of code.
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
echo "<center><table border='1' id='members'>";
echo "<tr>";
echo "<th>Parent ID</th>";
echo "<th>Parent Firstname</th>";
echo "<th>Parent Lastname</th>";
echo "<th>Parent Middlename</th>";
echo "<th>Child ID</th>";
echo "<th>Child Firstname</th>";
echo "<th>Child Middlename</th>";
echo "<th>Child Lastname</th>";
echo "<th>Action</th>";
echo "</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td>{$Parent_ID}</td>";
echo "<td>{$PFname}</td>";
echo "<td>{$PLname}</td>";
echo "<td>{$PMname}</td>";
echo "<td>{$Child_ID}</td>";
echo "<td>{$CFname}</td>";
echo "<td>{$CMname}</td>";
echo "<td>{$CLname}</td>";
echo "<td>";
echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records found.";
}
$result->free();
$mysqli->close();
I see two mistakes:
you should satisfy the right order of statements, $result should be
before $num_results assigment.
it seems that there is a mistake in your SQL query.
You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;

while loop data doesnt print on screen - PHP

Does anyone see an error in this? I dont get the results printed which is expected from the second loop. How ever the second loop headers are printed without an issue.
I'm trying to print data from a SQL database.
<?php
$con= new mysqli('localhost','root','','regional_data');
if (mysqli_connect_errno()) {exit('Connection failed: '. mysqli_connect_error());}
$result = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'");
echo "<table cellpadding='2' class='tablet' cellspacing='0'>";
echo
"<tr>
<th></th>"
."<th>"."Starting Cheque No"."</th>"
."<th>"."Ending Cheque No"."</th>"
."<th>"."Total No of Cheques remaining"."</th>"
."<th>"."Cheque Type"."</th>"
."</tr>";
while ($reca = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='checkbox' ></td>";
echo "<td>".trim($reca["sbstart"])."</td>";
echo "<td>".trim($reca["sbend"])."</td>";
echo "<td>".trim($reca["totsb"])."</td>";
echo "<td>SB</td>";
echo "</tr>";
}
echo "</table>";
echo "<table cellpadding='2' class='tablet' cellspacing='0'>";
echo
"<tr>
<th></th>"
."<th>"."Starting Cheque No"."</th>"
."<th>"."Ending Cheque No"."</th>"
."<th>"."Total No of Cheques remaining"."</th>"
."<th>"."Cheque Type"."</th>"
."</tr>";
while ($reca = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='checkbox' ></td>";
echo "<td>".trim($reca["gwstart"])."</td>";
echo "<td>".trim($reca["gwend"])."</td>";
echo "<td>".trim($reca["totgw"])."</td>";
echo "<td>GW</td>";
echo "</tr>";
}
echo "</table>";
$con->close();
?>
</div>
while ($reca = mysqli_fetch_array($result))
This fetches all results from the result set. After that the result set is exhausted, which is why the loop ever ends. There are no more results to be fetched from the same result set afterwards.
Either issue a new query, or save the data into an array which you can loop over again as many times as you want.
I don't think you can use the same $result variable twice.
What I would do is the following:
$result = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'");
$result2 = mysqli_query($con,"SELECT * FROM newchk WHERE dist_chk='$distUsr'");
Then your first while loop can use mysqli_fetch_array($result) and the second one can use mysqli_fetch_array($result2).
Hope this helps!

PHP - Pagination and place results is a table

I have tried my hand at some pagination but it gives me very unexpected results....
The first is that it on the first page of the pagination it seems to override the CSS and align all divs to the right hand side of the page... when you then click 'next' to view the next three results and so on the page goes back as per the CSS tells it!
The second problem is that the table displaying the results doesn't seem to work. The very first result on each page of the pagination is in the table, the next two results (three results per page) are displayed on the page however below and not in the table!
The code is quite long but here we go:
<?php
session_start();
if ( !isset($_SESSION['username']))
{
header("Location:index.php");
exit();
}
//connect to database
require "dbconn.php";
$per_page = 3;
$start = $_GET['start'];
$sort = #$_POST['order'];
if (!empty($sort)) {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC";
}
else {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY bookname ASC";
}
$results = mysql_query($query)
or die (mysql_error());
$record_count = mysql_num_rows($results);
if (!$start)
$start = 0;
$get = mysql_query("SELECT * FROM booktable LIMIT $start, $per_page");
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
Then I will cut out all the unnecesary html
<div id="mid">
<?php
echo "<table border='2px'>";
echo "<tr>";
echo "<th>";
echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
// get data
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
echo "</table>";
}
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start<=0))
echo "<a href='products.php?start=$prev'>Prev</a> ";
//set variable for first page number
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo "<a href='products.php?start=$x'>$i</a>";
else
echo "<a href='products.php?start=$x'><b>$i</b></a>";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo "<a href='products.php?start=$next'>Next</a>";
?>
<?php echo $record_count; ?>
This is how the page looks (image 1) (the table border shows the problem)
As I said before I also get the problem when the list first gets displayed and the page ends up looking like this: (image2)
You can see how they differ!
I hope that I have made sense!
For starters, you have your </table> tag in your while loop. Change that and see if everything else falls into place.

Categories