简体   繁体   English

单击<a>href链接</a>后如何插入数据库<a>?</a>

[英]How to insert into database after you click on <a> href link?

I have a link for download <a href="http://website/filename.pdf">Download</a> and I would like that everytime when someone clicks on Download I could insert into database total_downloads+1. 我有一个下载<a href="http://website/filename.pdf">Download</a>的链接,我希望每当有人点击下载时我都可以插入数据库total_downloads + 1。 For inserting into database I normally use 对于插入数据库,我通常使用

<form method="POST">
<input type="submit" name="download" value="Download">
</form>

and then 接着

if (isset($_POST['download'])) {...}

But I don't know how to trigger download after click on download button in form. 但是我在单击表单中的下载按钮后不知道如何触发下载。

The quick and easy way is: 快速简便的方法是:

header('Location: http://website/filename.pdf');

To have little more control over the parameters you can use this code: 要对参数进行更多控制,可以使用以下代码:

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="somename.pdf');
header('Content-Length: '.filesize($filepath) );
readfile($filepath);

Regarding the answer of kgm (which is absolutely correct), you should make sure that the files to download are fairly small, because readfile() reads all the file contents into memory, which could lead to exhausted memory. 关于kgm的答案(绝对正确),你应该确保要下载的文件相当小,因为readfile()将所有文件内容读入内存,这可能导致内存耗尽。 To avoid this, have a look at "readfile_chunked" in the user contributions on this site: http://php.net/manual/de/function.readfile.php 要避免这种情况,请查看本网站上用户贡献中的“readfile_chunked”: http//php.net/manual/de/function.readfile.php

cheers :) 欢呼:)

To make submit button work like a html link add onclick attribute to submit button 要使提交按钮像html链接一样工作,请添加onclick属性以提交按钮

<input type="submit" onClick="parent.location='http://website/filename.pdf'" 
 name="download" value="Download">
<a href="http://website/filename.pdf" onclick='$.post("name_of_page.php", {download: "Download"});'>Download</a>

我不确定报价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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