Displaying MySQL data in PHP Table Not working - php

Currently I'm creating just a simple website that I'm fooling around with. Users can add movies to watch, and then can view them later on. What my current problem is, is this.
Sorry for the large image. As you can see its displaying the first result correctly, but the second result gets all skrewy and displays at the top of the screen. My code for displaying the data is:
$result = mysql_query("SELECT * FROM `movies`");
echo "
<table id=\"allTable\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>ID</th>
<th>Movie</th>
<th>Genre</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
echo "</table>";
}
Any help would be greatly appreciated!
EDIT Fixed the problem right after I created this. Removed from while loop and put it under. Fixed.

echo "</table>"; should be moved outside of your while($row = mysql_fetch_array($result))
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
}
echo "</table>";

Related

PHP MYSQL - $_GET and Query Issue

I have a page which contains a table. The 'change_id' field contains hyperlinks which direct the user to a different page with an overview of that change_id's details.
'<td>' . $row['change_id'] . '</td>';
Now, just to test that the change_id is being received on the home2.php page, I used the following code:
<?php
include 'config.php';
$change_id=$_GET['change_id'];
print_r($_GET);
?>
This test successfully displayed the correct change id's:
Array ( [changeid] => 1006 )
Now, when I go to query the SQL Database using the change_id it doesn't work as desired.
<?php
include 'config.php';
$change_id=$_GET['change_id'];
$query1 = "SELECT * FROM `change_request_tbl` WHERE `change_id` = $change_id";
$result = mysqli_query($conn, $query1);
echo "<fieldset><legend><strong>New Requests:</legend></strong>
<table border=4 bordercolor=black class=table>
<tr>
<th>Change ID:</th>
<th>Customer Name:</th>
<th>Change Requestor:</th>
<th>Date CR raised:</th>
<th>CPM/Ticket:</th>
<th>Out of Hours:</th>
<th>Change Category:</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['change_id'] . "</td>";
echo "<td>" . $row['customer_name'] . "</td>";
echo "<td>" . $row['change_requestor'] . "</td>";
echo "<td>" . $row['date_cr_raised'] . "</td>";
echo "<td>" . $row['cpm_ticket'] . "</td>";
echo "<td>" . $row['out_of_hours'] . "</td>";
echo "<td>" . $row['category_of_change'] . "</td>";
echo "</tr>";
}
echo "</table></fieldset><br><br><br>";
?>
The table are headers are shown without any data. Any ideas on how to fix? Thanks in advance
You are setting $change_id to $_GET['change_id']
However you are passing $_GET the parameter name of changeid
If you change
$change_id = $_GET['change_id'];
To
$change_id = $_GET['changeid'];
It should work as expected :)

For loop not working in PHP script

I have written a PHP script to pull data from a SQL database to display on a webpage. Everything seems fine but when I run the script it throws the error:
Parse error: syntax error, unexpected ')', expecting ';' in db_data.php on line 21
Line 21 is my FOR loop, I have checked the syntax of the loop and it seems to be correct so I can't understand why it is failing.
$result = mysqli_query($con,"SELECT * igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
for($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Change for to while
<?php
// Missing FROM here vv
$result = mysqli_query($con,"SELECT * FROM igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
As Anthony Thompson and RJParick says, you have to change for to while. The error you're encountering is due tu for that need 3 params like this :
for($i; $i<10; $i++) {//Note the ; used to separate params.
//do something
}
So your loop would begin like this :
while($row = mysqli_fetch_array($result)){
Another detail, your SQL isn't correct use SELECT * FROM igi instead.

Table creation with php and mysqli

i'm trying to do a table with php which takes the data from my database and build's the table im html but despite my while loop , only the 1st row is getting into the table. If i change the while loop before the
echo "<table border='1' cellpadding='2' cellspacing='2'";
I get all of them but in 3 tables.
What's wrong with the code ?
<?php
require 'connect.php';
$query = $link->query("SELECT * FROM fornecedor");
echo "Fornecedores";
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr>
<td>Nome</td>
<td>NIF</td>
<td>Cidade</td>
<td>Rua</td>
<td>NrPorta</td>
<td>Website</td>
<td>email</td>
</tr>";
while ($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row["Nome"] . "</td>";
echo "<td>" . $row["NIF"] . "</td>";
echo "<td>" . $row["Cidade"] . "</td>";
echo "<td>" . $row["Rua"] . "</td>";
echo "<td>" . $row["NrPorta"] . "</td>";
echo "<td>" . $row["Website"] . "</td>";
echo "<td>" . $row["Email"] . "</td>";
echo "</tr>";
echo "</table>";
};
?>
Move echo "</table>"; out of the while loop-
....
while(){
.....
// don't close the table tag here
}
echo "</table>";
For each row you are adding the closing </table> tag which should not be like that.
echo "</table>"; code must be out of the while loop. It must be written once only.

Adding php variable into a href link into a table

I am working on uploading a file along with a description and table and then displaying it in a table format. My problem is that I'm not sure how to link my the path for the uploaded file into the table so the user can click on the link in the table and it will download.
Code:
This is what I'm attempting to use>
<?php
include 'connect.php';
$result = mysqli_query($con,"SELECT DocDate, Description, DocFile FROM Documents");
echo "<table border='0' width='100%'>
<col width='50'>
<col width='100'>
<tr>
<th>Date</th>
<th>Description</th>
<th>File</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" $row['DocFile'] "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
If you feel it to be usefull I'm happy to add the code where I upload the file to my server.
EdiT Sorry I put in the wrong variable thing into my table, I don't think it changes it too much
Is this what you're looking for?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td> " . $row['DocFile'] . " </td>";
echo "</tr>";
}
This is how you can do
echo '<td>'.$name.'</td>';
I prefer to use ' ' and within it have the HTML since HTML will contain a lot of "" so no need to use escape them and then separate PHP and HTML with concatenation.
you have syntax error in this line in while loop:
echo "<td>" $name "</td>";
should be :
echo "<td> $name </td>";
In this line you have an error:
echo "<td>" $row['DocFile'] "</td>";
You need to scape the " character:
echo "<td> " . $row['DocFile'] . " </td>";
With your syntax you are creating an error because you are ending the string after the td tag.
If you are using a web url which uses a php variable in the url and want to open a new tab when you click on the hyper link, use this
echo "<td><a target='_blank' href=\"http://view.php?Id=".$row['Id']."\">". $row['Id'] ."</a></td>";
This is helpful when you have to use a php variable in the hyperlink and is being used in a table. Clicking on the Id will open the page related to that Id in a new tab in this case.

How to make string/variables from query results into a link in php

best regards.
I know perhaps some's considering my question is quite stupido. But I've been trying looking for to this simple scripting. As my post title above, how to make the query results into a url. I have this scripts:
$result = mysqli_query($con,'SELECT......');
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Category</th>
<th>Link</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['author_name'] . "</td>";
echo "<td>" . $row['publisher_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['url_flipbook'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to make the result from $row ->'url_flipbook' into a url. The $row -> 'url_flipbook' will produce a html pages that users can click on it, it's located to a folder in my localhost.
The database field:
| url_flipbook |
-----------------
/myspace/click-it/info_1.html
I want that query results became a link for the output.
I've tried:
<a href=''echo $row['url_flipbook'];'?>''>FLIPBOOK</a>"</td>";
echo "<td>" . <a href='$row['url_flipbook']>FLIPBOOK</a>"</td>";
Nothing works...if you could help me to solve this problems. Thank you so much...
best regards,
Kris
Just concat the string to the href:
echo "<td>FLIPBOOK</td>";
or alternatively:
echo '<td>FLIPBOOK</td>';
did u try
echo "<td>" . "<a href='" . $row['url_flipbook'] . "' >FLIPBOOK</a></td>";
try this
echo "<td><a href='".$row['url_flipbook']."'>FLIPBOOK</a></td> ";

Categories