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

2 つのクライアントが同じイメージを同時にダウンロードすると、テーブルの読み取りと更新が重複する可能性があります。どちらも SELECT クエリで同じ値を読み取り、それに 1 を追加してから、同じ新しい値で UPDATE します。

PHP で 1 を追加する代わりに、データベース自体にそれを実行させます。

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

ロックとトランザクションを使用してクライアントでこの問題を完全に解決することは可能ですが、この場合は 1 つのクエリで行う方が簡単です。

また、新しいコードで mysql_* 関数を使用しないでください。これらは推奨されていません。mysqli_* または PDO に切り替え、準備済みステートメントを使用して SQL インジェクションを回避してください。

于 2012-09-17T06:56:21.037 に答える
2
  1. mysql_*関数の使用は非推奨です。mysqli またはPDOを使用してコードを書き直します。

  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 に答える