简体   繁体   中英

Display field in MySQL as a link in PHP?

I have a submission script set up where people can submit their details and upload an attachment. It is all recorded in a database in a table called Submissions with the following fields: fname , lname , snumber and upload .

The upload field is currently the location of the file on the server, for example one entry says uploads/123456.zip (uploads is the directory, 123456.zip is the file name).

I also have a back end administration system where all the submissions are displayed. The code used for that is as follows:

<html>
<head>
<title>Internal Database Listing</title>
<style type="text/css">
/**** Start page styles ****/

body {
    background: #DFA01B;
    font-family: arial, sans-serif;
    font-size: 14px;
    }

#wrap {
    max-width: 900px;
    margin: 30px auto;
    background: #fff;
    border: 4px solid #FFD16F;
    -moz-border-radius: 15px;
    -webkit-border-radius: 15px;
    border-radius: 15px;
    padding: 20px;
        }
table.subs {
text-align: center;
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif ;
font-weight: normal;
font-size: 11px;
color: #fff;
width: 800px;
background-color: #666;
border: 0px;
border-collapse: collapse;
border-spacing: 0px;
}

table.subs td 
{background-color: #CCC;
color: #000;
padding: 4px;
text-align: left;
border: 1px #fff solid;}

table.subs td.hed
{background-color: #666;
color: #fff;
padding: 4px;
text-align: left;
border-bottom: 2px #fff solid;
font-size: 12px;
font-weight: bold;}

</style>
</head>
<body>
<?php
$username="dbuser";
$password="dbuserpass";
$database="dbsub";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM Submissions";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<div id="wrap">
    <div align="left">
        <img src="logo.png" width="208" height="87" />
    </div>
    <div align="center">    
        <h1 style="font-family:arial">Total submissions</h1>
    </div>
<div align="center">
<table class="subs" border="1" cellspacing="1" cellpadding="3">
<tr>
<th><font face="Arial, Helvetica, sans-serif">First name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Last name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Student #</font></th>
<th><font face="Arial, Helvetica, sans-serif">Grade</font></th>
<th><font face="Arial, Helvetica, sans-serif">Submission</font></th>
</tr>
</div>
</div>
<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"fname");
$f2=mysql_result($result,$i,"lname");
$f3=mysql_result($result,$i,"snumber");
$f4=mysql_result($result,$i,"grade");
$f5=mysql_result($result,$i,"upload");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
</tr>

<?php
$i++;
}
?>
</body>
</html>

This code works perfectly except under the upload field it displays upload/123456.ppt. How do I get it to display http://sitename.com/uploads/123456.ppt AND make it a hyper link to that file?

只需将输出包装在<a>标记中

<td><font face="Arial, Helvetica, sans-serif"><a href="http://sitename.com/<?php echo $f5; ?>"><?php echo $f5; ?></a></font></td>

The following will print a link containing the fixed URL.

<?php 
$url = 'http://www.' . $_SERVER['SERVER_NAME'] . '/' . $f1;
echo '<a href="' . $url . '">Click here to download the file!</a>';

On the off-chance that $_SERVER['SERVER_NAME'] is not correct for your website, you can simply replace it with the correct domain name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM