php没有更新我的数据库

时间:2012-09-17 06:43:18

标签: php mysql database

我有这个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;

?>

3 个答案:

答案 0 :(得分:3)

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

不要在PHP中添加1,而是让数据库自行完成:

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

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

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

答案 1 :(得分:2)

  1. 不推荐使用mysql_ *函数。使用mysqli重写您的代码 或PDO

  2. mysql_error说什么?

  3. 以下代码无法访问:

  4. $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");
    }
    

答案 2 :(得分: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;

?>