简体   繁体   中英

php is not updating my database

I have this php page, which suppose to display an image and then update "download" row in database with 1 number for each visit, some times this works , and for some other pictures it's not working, how can i fix this ? please take a look at my code if there's something wrong :

<?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;

?>

If two clients download the same image at the same time, they may overlap in reading and updating the table. They'll both read the same value in the SELECT query, add 1 to it, and then UPDATE with the same new value.

Instead of adding 1 in PHP, let the database do it itself:

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

It's possible to solve this problem entirely in the client by using locks and transactions, but in this case it's simpler to just do it in one query.

Also, you should not use the mysql_* functions in new code, they have been deprecated. Please switch to mysqli_* or PDO, and use prepared statements to avoid SQL injection.

  1. use of mysql_* functions is deprecated. Rewrite your code with mysqli or PDO .

  2. What does mysql_error say?

  3. Following code is unreachable:

.

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

Try this its working :) you add wrong code

<?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;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM