我已经在 MySQL 和 Apache 服务器上使用 PDO 和 PHP 一段时间了。我最近的任务是将企业的旧 Web 应用程序转换为新设置。旧设置是标准 Linux Web 堆栈 (Apache/PHP/MySQL/Filezilla),新设置将是带有 IIS/PHP(快速 cgi 安装)/ SQL Server 2003/无 FTP 的 Windows Server 2003。
除了转换 MySQL 语句以更新包含文件访问信息的表之外,我几乎一切都在工作。使用带有 SQLSRV 驱动程序的 PDO 并在“文件下载”PHP 脚本内执行插入语句,将多条记录插入到 SQL 表中。
download.php 确实向 SQL 服务器发出多个查询。一次检查一个表中的文件是否存在和变量,然后使用访问信息更新另一个表。
请参阅下面的 download.php 代码
调试显示 $count 的 print/echo 为 1。然而,检查 SQL Server 记录,总是显示插入的不止一个。有时它只是额外的一行,总共两行,但其他时候则多达四个额外的语句被插入。在每种情况下,$count 都保持显示为 1。
这个特定的 PHP 脚本在调用此插入语句之前根据 SQL 数据库验证信息。首先,验证文件访问的身份验证(成功),验证文件存在(成功),然后使用下载信息更新访问表(错误),最后将 PDF 提供给用户(成功)。
当我向查询分析器手动发出 INSERT 语句时,它成功并按预期工作;它每次插入一行。该错误似乎与execute() 的SQLSRV 或PDO 实现有关。
我在 stackoverflow、serverfault 和万能的 Google 上搜索了有关此的信息。用户需要在一个语句/执行中执行多个查询/插入时返回的唯一结果类型。我的问题在哪里是相反的;我希望只执行一条插入语句,但是总是会执行多个插入语句。
问题是:为什么会发生这种情况以及如何防止发生多次插入?
根据请求更新
访问此文件的代码是来自另一个网页的单个链接。该页面列出了允许用户访问的当前文件,并提供了 download.php 脚本的链接,用于验证、更新和实际提供 PDF。
查看页面有一个链接列表(以 for 循环打印),排列如下:
<a href='download.php?f={$item['name']}&t={$type}' target='_blank'>{$item['name']}</a>
当用户单击该链接时,除了上面 download.php 的其他代码之外,还会运行下面的脚本。它成功地提供了 PDF 文件。内容由 download.php 作为 PHP 标头/内联 PDF 发送:
请参阅下面的代码
查看服务器日志显示two对 download.php 文件的 GET 请求:
2012-02-14 17:44:37 W3SVC1785071458 172.17.31.254 GET /download.php f=06304844-1A.pdf&t=av 4090 - 172.17.31.112 Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/535.7+(KHTML,+like+Gecko)+Chrome/16.0.912.77+Safari/535.7 200 0 0
2012-02-14 17:44:37 W3SVC1785071458 172.17.31.254 GET /download.php f=06304844-1A.pdf&t=av 4090 - 172.17.31.112 Mozilla/5.0+(Windows+NT+6.1)+AppleWebKit/535.7+(KHTML,+like+Gecko)+Chrome/16.0.912.77+Safari/535.7 200 0 0
我在 Firefox、Opera 和 IE (6-9b) 中进行了测试,结果是相同的。
更新二
将整个 download.php 文件放在这里:
<?php
session_start();
require("cgi-bin/auth.php");
// Don't timeout when downloading large files
@ignore_user_abort();
@set_time_limit(0);
//error_reporting(E_ALL);
//ini_set('display_errors',1);
function getfile() {
require('cgi-bin/connect_db_pdf.php');
//Verify information
if (!isset($_GET) || !isset($_GET['f']) || !isset($_GET['t'])) {
echo "Nothing to do!";
exit(0);
}
//Update variables
$vuname = strtolower(trim($_SESSION['uname']));
$file = trim($_GET['f']); //Filename we're looking for
$type = trim($_GET['t']);//Filetype
if (!preg_match('/^[a-zA-Z0-9_\-\.]{1,60}$/', $file) || !preg_match('/^av|ds|cr|dp$/', $type)) {
echo "Non conforming values";
exit(0);
}
try {
$sQuery = "SELECT * FROM pdf_info WHERE PDF_name=:file AND PDF_type=:type";
$statm = $conn->prepare($sQuery);
$statm->execute(array(':file'=>$file,':type'=>$type));
$result = $statm->fetch();
$count = $statm->rowCount();
$sQuery = null;
$statm = null;
if ($count == 1 ){ //File was found in the database so let them download it. Update the time as well
$sQuery = "INSERT INTO access (PDF_name,PDF_type, PDF_time, PDF_access) VALUES (:file, :type, GetDate(), :vuname)";
$statm = $conn->prepare($sQuery);
$statm->execute(array( ':vuname'=>$vuname, ':file'=>$file, ':type'=>$type));
$count = $statm->rowCount();
$sQuery = null;
$statm = null;
$sQuery = "UPDATE pdf_info SET last_view=GetDate(),viewed_uname=:vuname WHERE PDF_name=:file AND PDF_type=:type";
$statm = $conn->prepare($sQuery);
$statm->execute(array( ':vuname'=>$vuname, ':file'=>$file, ':type'=>$type));
$sQuery = null;
$statm = null;
//$result is from FIRST SELECT query outside this 'if' scope.
$file_loc = $result['floc'];
$file_name = $result['filename'];
$fileh = fopen($file_loc,'rb');//Send content to browser as inline PDF
header("Content-Type: application/pdf");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . filesize($file_loc));
header("Accept-Ranges: bytes");
header("Content-Disposition: inline; filename={$file_name}");
while (!feof($fileh)) {
echo(@fgets($fileh, 8192));
}
fclose ($fileh);
exit(0);
} else { //We did not find a file in the database. Redirect the user to the view page.
header("Location: view.php");
}
} catch(PDOException $err) {//PDO SQL error.
//echo $err;
header('Location: error.php');
exit(0);
}
}
getfile();
?>