将下载文件的IP地址插入mysql [英] Insert IP address of downloaded files into mysql

查看:86
本文介绍了将下载文件的IP地址插入mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此问题我有2个表Sourcedetails.

Source表如下:

+----+----------+---------------+-----------+
| id | item_name|items_download | category  |
+----+----------+---------------+-----------+
|                                           |
+----+----------+---------------+-----------+

details表如下:

+------+----------+-----+------+
| name | download | time| ip   |
+------+----------+-----+------+
|                              |
+------+----------+-----+------+

第一步,我想从源"表中实时获取数据,并通过以下代码放入详细信息表中:

At first step I want to get data from Source table (in real time) and put into details table by this code:

$get= "INSERT INTO `details` (`name`,  `download`) SELECT `Source`.`item_name`,`Source`.`items_download` FROM `Source`"

下一步,我想获取每个文件的访问者IP地址.

At next step I want to get visitor IP address for each file.

例如,如果有人下载了testfile,我希望获得以下输出:

for example if someone downloaded testfile I want to have this output:

+----------+---------+--------------+-----------+
|  name    | download |   time      |  ip       |
+----------+----------+-------------+-----------+
| testfile |     32   |download time|192.168.0.0|
+----------+----------+-------------+-----------+
| file2    |     0    |             |           |
+----------+----------+-------------+-----------+

为此,我使用以下代码:

To do this i use this code:

$ip = $_SERVER['REMOTE_ADDR'];
$update = "UPDATE details SET  ip = '$ip', dldate = NOW()"

但是它发生在所有文件上,所有文件都具有相同的IP和时间.我知道它需要一个条件WHERE,但是我不知道应该输入什么条件来获取每个下载文件的IP地址.

But its happened for all files, all of the file get same IP and time. I know its need a condition WHERE but I don't know what should I type as a condition to get IP address for each file that download.

推荐答案

您应该使用在第一个表格视图中使用的id列,但在随后的视图中逐步消失.

You should use your id column, which you have in your first table view, but it stragely disappears in the later ones.

您的id列也应该是您的PRIMARY_KEY AUTO INCREMENT列.然后,每一行都有其自己的唯一索引ID. 但是除此之外,您还可以使用什么来识别用户下载的文件?如果是文件名,则只需使用该文件名:

Your id column should also be your PRIMARY_KEY AUTO INCREMENT column. Then each row has its own unique and indexed id. But aside from that, what do you use to identify which file the user downloads? If it's the filename then simply use that:

details表中添加id列!

$update = "UPDATE details SET ip = '$ip', dldate = NOW() 
WHERE name = '$fileNameValue' LIMIT 1"

在相关说明中,您可以使用device = ip = '$ip'的相同速记引用来更新多列,您必须单独指定每列并插入绝对目标数据(此处为变量).

On a related note, you can not update multiple columns with the same shorthand reference of device = ip = '$ip' you have to specify each column in isolation and with an absolute target data to insert (here, the variable) .

如果是错字,则应编辑并更新您的问题.

If this is a typo you should edit and update your question.

也请参阅Gerfried关于使用准备好的语句的答案,这是您应该执行这些操作的方式.

Please also see Gerfried's answer regarding using prepared statements, they are the way you should be doing these things.

这篇关于将下载文件的IP地址插入mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆