问题
Hi I want to show URL as a link in PHP the URL is shown by query from database but it is not a link so I want to make it link like using <a href=""></a> but I don't know what I am doing wrong
My data show like this in browser
ID Name URL
2 This localhost/p_uploads/00.jpg
3 Nissan localhost/p_uploads/7a.jpg
I want these URL's to be link so anyone can click on the url to open the image
Here is my PHP Code:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"Select * from private_uploads where username = '".$_SESSION['user']."'")
or die(mysql_error());
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
echo "</tr>";
}
echo "</table>";
//Views Counter
mysqli_close($con);}
?>
<?php
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>back</a>";
?>
回答1:
replace
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
for this
echo '<td><a href="' . $row['Link'] . '">[a name here would be nice]</a></td>';
you are missing the "" in the generated href
回答2:
You're producing links that look like this:
<a href="localhost/p_uploads/00.jpg"></a>
That's both an invalid URL (unless you really do have a localhost/p_uplaods folder) and a link with no body, so you'll never see it.
If you want to access localhost the host, and not the folder, you should use use an absolute path on the current host, /p_uploads/00.jpg, or a fully qualified URL: http://localhost/p_uploads/00.jpg.
回答3:
Your link looks like this:
<a href="localhost/p_uploads/00.jpg"></a>
It doesn't have a HTTP protocol. The link should look like this:
<a href="http://localhost/p_uploads/00.jpg"></a>
回答4:
You might have a problem on this line:
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
As you are referring to Link whereas in your database the column is called URL. Also add a text to the tag:
echo '<td><a href="http://' . $row['URL'] . '">View</a></td>';
来源:https://stackoverflow.com/questions/20287687/how-to-make-link-of-the-database-in-php