Jump to content

MySql query updates twice

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Im running the following query in a php file :

<?php
header('Content-Type: image/png');
$official = "myImage.png";
if(!isset($_GET['url']))
    $imageurl = $official;
else
    $imageurl = $_GET['url'];

include 'dbc.php';
if($imageurl != $official)
{
    $result = mysqli_query($myLink, "SELECT * FROM bannerimages WHERE url='".$imageurl."'");
    if(mysqli_num_rows($result) != 0)
    {
        $bdata = mysqli_fetch_array($result);
        if($bdata['status'] == 1)
            $imageurl = $official;
    }
    else
        mysqli_query($myLink, "INSERT INTO bannerimages SET url='".$imageurl."', status=0");
}

if(isset($_GET['id']))
    mysqli_query($myLink, "UPDATE users SET credits=credits+1 WHERE id='".$_GET['id']."'");

$suffix = substr($imageurl, strlen($imageurl) - 3);
switch(strtolower($suffix))
{
    case "jpg":
        $image = imagecreatefromjpeg($imageurl);
        break;
    case "png":
        $image = imagecreatefrompng($imageurl);
        break;
    default:
        imagecreatefrompng($official);
        break;
}
imagepng($image, NULL, 9);
?>
the idea is that it is a banner for a website where the user can customize the image if they want to and add one credit to their account every time someone shows the image. The code works perfectly except the fact that it updates the credit field with twice the amount. If I code it to update it with 50 it adds 100, if I code it to add 100 it adds 200...

What am I doing wrong?

Edited by hampus.tagerud, 18 October 2010 - 04:47 AM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Your code only updates the credit to go down one each view, what are you using to update the credits to what you had said?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
My mistake, it should be:

if(isset($_GET['id']))
    mysqli_query($myLink, "UPDATE users SET credits=credits+1 WHERE  id='".$_GET['id']."'");
It was removing one since I wanted to see if it was doing the same if I wanted to remove credits... Which it did. I've changed the code in the first post to look like the on in this post!

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I may need coffee but I can see nothing out of the right in your script, you may wish to comment out the header() and imagepng() call, and run two queries before and after your update, and verify it is actually doubling with that single call. You may be calling the image twice from somewhere else.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Since im only loading the php file like this: http://www.mydomain....randomimage.png in my browser I only load it twice. But is it possible that the browser somehows executes this twice? Is it in that case possible to check if the browser is opening the file for the first time or not? Maybe checking the referrer or something?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I would still write to a log to see if it does actually load twice, try adding this code:
$fp = fopen('bannerlog.txt', 'a');
fwrite($fp, date('h:m:s') . ': Banner was called with ' . $_GET['url'] . ' for ID ' . $_GET['id'] . "\n");
fclose($fp);
If it truly is running twice, then I do not know what on earth the server or browser is doing, it may contain broken output buffering or is run on a faulty GD version. You may need to add a throttle, so it can only update credit once per minute or something.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
hampus.tagerud

hampus.tagerud

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
It is neither the browser or the server cause your code only wrote one line...
I guess that it is about time to give up on this now since there's nothing wrong with the code or the browser... Thank you for your help Nullw0rm!