简明现代魔法 -> PHP服务器脚本 -> PHP 文件下载次数记录
PHP 文件下载次数记录
2010-03-05
设计原理如下:
- 用户点击下载的 link,通过 url 传一个 file_id 值给 download.php 页面。
- download.php 获取该 url 值,然后通过该值查找该数据库记录。然后修改该记录中的 download 计数字段。
- 重定向到下载文件。
下载链接如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <div><a href="download.php?file_id=1">下载</a></div>
数据库设计
create table filecount
(
count_ID bigint(20) unsigned not null auto_increment primary key,
file_title varchar(50),
file_name varchar(30) not null,
file_type varchar(20) not null,
download_times varchar(40) not null,
create_date date not null default '0000-00-00'
)default charset=utf8;
PHP code
<?php
$file_id = $_GET['file_id'];
//echo $file_id.'<br />';
require('includes/dbinfo.php');
// 连接到 MySQL 服务器
$connection = mysql_connect ($host, $username, $password);
//echo $username.'<br />';
// 设置活动的 MySQL 数据库
$db_selected = mysql_select_db($database, $connection);
// 查询该 ID 值的数据库列
$query = mysql_query("select * from filecount where count_ID = $file_id");
// 将字段值读入到 $myrow 数组
$myrow = mysql_fetch_array($query);
$file = $myrow["file_name"];
//echo $file.'<br />';
$times = $myrow['download_times'] + 1;
//echo $times.'<br />';
// 更新下载次数
$query1 = "update filecount set download_times = '$times' where count_ID = $file_id ";
// 跳转到该下载文件
$result = mysql_query($query1);
if ($result)
{
echo "<script language='javascript'>window.location='pdf/$file';</script>";
}
?>
写 SQL 修改某值
<?php
require('includes/dbinfo.php');
// 连接到 MySQL 服务器
$connection = mysql_connect ($host, $username, $password);
//echo $username.'<br />';
// 设置活动的 MySQL 数据库
$db_selected = mysql_select_db($database, $connection);
$times = "简明现代魔法";
// 更新下载次数
$query1 = "update filecount set file_title = '$times' where count_ID = 1 ";
$result = mysql_query($query1);
?>
dbinfo.php
<? $username="root"; $password=""; $database="nowamagic"; $host="localhost"; ?>

