0

我有这个 php 页面,它假设显示一个图像,然后在每次访问时使用 1 个数字更新数据库中的“下载”行,有时这有效,而对于其他一些图片它不起作用,我该如何解决这个问题?如果有问题,请查看我的代码:

<?php

include 'conf_global.php';

if ($_GET['id'])
{
    $id = $_GET['id'];
}
else
{
    die ("no id selected");
}

@ $db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error());
//IT"S NOT WORKING!
if (!$db)
{
    die("error");
}
mysql_select_db($mysql['db']) or die(mysql_error());

$query = "SELECT * FROM `images` WHERE id='" . $id ."'";

$result = mysql_query($query) or die(mysql_error());

if (!$result)
{
    die("MySQL Select error");
}

$num_results = mysql_num_rows($result);
if ($num_results ==0)
{
die("Image not found");
}

$row = mysql_fetch_array($result);
if ($id != 0)
{
    $downloads = $row['downloads'] + 1;

    $lastuse = time();

     $ss = mysql_query("select downloads from `images` where id='".$id."'") or die(mysql_error());
     $rr = mysql_fetch_array($ss);

    $query = "update `images` set downloads='".($rr['downloads']+1)."' where id='".$id."'";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL update error");
    }

    $query = "UPDATE `images` SET lastuse='" . $lastuse . "' WHERE id='". $id ."'";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL update error2");
    }

    //get current stats
    $query = "SELECT * FROM `stat_cache`";

    $result = mysql_query($query);

    if (!$result)
    {
        die("MySQL Select error");
    }
    $stat = mysql_fetch_array($result);

    //band update
    $bandwidth = $stat['band'] + $row['size'];
    $query = "UPDATE `stat_cache` SET band='" . $bandwidth . "' WHERE 1";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL Update error");
    }

    //downloads update
    $downloads = $stat['downloads'] + 1;
    $query = "UPDATE `stat_cache` SET downloads='" . $downloads . "' WHERE 1";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL Update error");
    }
}
//Lets create the image, now.
if(!file_exists('./uploads/' . $id)) {
    die("Image not found");
    exit;
}
header('Content-type: ' . $row['type']);

$fp = fopen('./uploads/' . $id, 'r');

$contents = fread($fp, $maxfilesize);
fclose($fp);

echo $contents;

?>
4

3 回答 3

3

如果两个客户端同时下载相同的图像,它们可能会在读取和更新表时重叠。他们都将在 SELECT 查询中读取相同的值,将其加 1,然后使用相同的新值进行更新。

与其在 PHP 中加 1,不如让数据库自己做:

$query = "update `images` set downloads=downloads+1, lastuse='" . $lastuse . "' where id='".$id."'";

可以通过使用锁和事务在客户端完全解决这个问题,但在这种情况下,只在一个查询中完成它会更简单。

此外,您不应在新代码中使用 mysql_* 函数,它们已被弃用。请切换到 mysqli_* 或 PDO,并使用准备好的语句来避免 SQL 注入。

于 2012-09-17T06:56:21.037 回答
2
  1. 不推荐使用 mysql_* 函数。用mysqliPDO重写你的代码。

  2. mysql_error 说什么?

  3. 以下代码无法访问:

.

$db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error());

//You never come here is mysql_pconnect failed because already called die();

//IT"S NOT WORKING!

if (!$db)
{
    die("error");
}
于 2012-09-17T06:51:14.110 回答
2

试试这个它的工作:)你添加了错误的代码

<?php

include 'conf_global.php';

if ($_GET['id'])
{
    $id = $_GET['id'];
}
else
{
    die ("no id selected");
}

@ $db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error());
//IT"S NOT WORKING!
if (!$db)
{
    die("error");
}
mysql_select_db($mysql['db']) or die(mysql_error());

$query = "SELECT * FROM `images` WHERE id='" . $id ."'";

$result = mysql_query($query) or die(mysql_error());

if (!$result)
{
    die("MySQL Select error");
}

$num_results = mysql_num_rows($result);
if ($num_results ==0)
{
die("Image not found");
}
else {
    $row = mysql_fetch_array($result);

    $downloads = $row['downloads'] + 1;

    $lastuse = time();

     $ss = mysql_query("select downloads from `images` where id='".$id."'") or die(mysql_error());
     $rr = mysql_fetch_array($ss);

    $query = "update `images` set downloads='".($rr['downloads']+1)."' where id='".$id."'";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL update error");
    }

    $query = "UPDATE `images` SET lastuse='" . $lastuse . "' WHERE id='". $id ."'";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL update error2");
    }

    //get current stats
    $query = "SELECT * FROM `stat_cache`";

    $result = mysql_query($query);

    if (!$result)
    {
        die("MySQL Select error");
    }
    $stat = mysql_fetch_array($result);

    //band update
    $bandwidth = $stat['band'] + $row['size'];
    $query = "UPDATE `stat_cache` SET band='" . $bandwidth . "' WHERE 1";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL Update error");
    }

    //downloads update
    $downloads = $stat['downloads'] + 1;
    $query = "UPDATE `stat_cache` SET downloads='" . $downloads . "' WHERE 1";
    $result = mysql_query($query);
    if (!$result)
    {
        die("MySQL Update error");
    }
}
//Lets create the image, now.
if(!file_exists('./uploads/' . $id)) {
    die("Image not found");
    exit;
}
header('Content-type: ' . $row['type']);

$fp = fopen('./uploads/' . $id, 'r');

$contents = fread($fp, $maxfilesize);
fclose($fp);

echo $contents;

?>
于 2012-09-17T18:27:45.180 回答