在MySQL中显示字段作为PHP中的链接?

时间:2011-10-12 23:48:54

标签: php mysql html file-upload

我设置了提交脚本,人们可以在其中提交详细信息并上传附件。它全部记录在名为Submissions的表中的数据库中,其中包含以下字段:fnamelnamesnumberupload

upload字段当前是服务器上文件的位置,例如,一个条目显示uploads/123456.zip(上传是目录,123456.zip是文件名)。

我还有一个后端管理系统,其中显示所有提交。用于此的代码如下:

<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>

此代码完美无缺,但在上传字段下显示upload / 123456.ppt。如何让它显示http://sitename.com/uploads/123456.ppt并使其成为该文件的超链接?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

以下内容将打印包含固定网址的链接。

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

如果$ _SERVER ['SERVER_NAME']不适合您的网站,您可以使用正确的域名替换它。