奇怪的问题。至少对于我来说。当点击 pdf.php 的链接时,查询会将某个文档被点击的次数写入 mysql 数据库。
问题:每次点击在数据库中不是写入“1”,而是写入“2”。看来php文件运行了两次。但为什么呢?
$file = './'.mysqli_real_escape_string ($con,$_GET[name]).'';
$filename = mysqli_real_escape_string ($con,$_GET[name]); /* Note: Always use .pdf at the end. */
$id = intval($_GET[id]);
$date = time();
$what = mysqli_real_escape_string ($con,$_GET[name]);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
$sql = "SELECT id,what,person_id,count FROM pdftrack WHERE id = '".$id."' AND what='".$what."'";
$ergebnis = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($ergebnis);
if ($row[id] == '')
{
$sql = "INSERT INTO pdftrack (id,what,date,count) VALUES ('".$id."', '".$what."','".$date."','1')";
mysqli_query($con,$sql);//echo $sql;
}
else
{
$count = $row[count]+1;
$sql="UPDATE pdftrack
SET count='".$count."',
date2='".$date."'
WHERE id='".$row['id']."'
";
mysqli_query($con,$sql);
}
更新:感谢您的帮助,但仍然算错。如果 header 部分消失了,它就可以正常工作。所以我认为这与标题部分有关。有什么想法吗?
解决方案:仅使用
header('Content-type: application/pdf');
@readfile($file);
目前看不到任何缺点。
最佳答案
此外,还有 mir da sofort auffällt:Du hast eine falsche 语法! Wenn du auf ein assoziatives Array zugreifst (in deinem Fall $row) dann musst du den Key auch als String deklarieren. Sprich statt $row[id] 必须遵守$row['id'] 版本。 Dieser Fehler ist z.B. bei deiner ersten if-Abfrage vorhanden。 Desweiteren bei else $row[count] durch $row['count'] ersetzen。
关于使用 header 在浏览器中显示 pdf 时,php 文件运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20240794/