2010-05-22 84 views
1

下面的代码首先是客户端代码,然后是类文件。PHP:MySQL查询无故重复更新

由于某些原因,'deductTokens()'方法调用了两次,因此对帐户收取了双倍的费用。 我一直在编程通宵,所以我可能只需要第二双眼睛:

if ($action == 'place_order') { 

    if ($_REQUEST['unlimited'] == 200) { 

     $license = 'extended'; 

    } else { 

     $license = 'standard'; 

    } 

    if ($photograph->isValidPhotographSize($photograph_id, $_REQUEST['size_radio'])) { 

     $token_cost = $photograph->getTokenCost($_REQUEST['size_radio'], $_REQUEST['unlimited']); 
     $order = new ImageOrder($_SESSION['user']['id'], $_REQUEST['size_radio'], $license, $token_cost); 
     $order->saveOrder(); 
     $order->deductTokens(); 
     header('location: account.php'); 

    } else { 

     die("Please go back and select a valid photograph size"); 

    } 

} 


######CLASS CODE####### 
<?php 

include_once('database_classes.php'); 

class Order { 

    protected $account_id; 
    protected $cost; 
    protected $license; 

    public function __construct($account_id, $license, $cost) { 

     $this->account_id = $account_id; 
     $this->cost = $cost; 
     $this->license = $license; 

    } 

} 

class ImageOrder extends Order { 

    protected $size; 

    public function __construct($account_id, $size, $license, $cost) { 

     $this->size = $size; 

     parent::__construct($account_id, $license, $cost); 

    } 

    public function saveOrder() { 

     //$db = Connect::connect(); 
     //$account_id = $db->real_escape_string($this->account_id); 
     //$size = $db->real_escape_string($this->size); 
     //$license = $db->real_escape_string($this->license); 
     //$cost = $db->real_escape_string($this->cost); 

    } 

    public function deductTokens() { 

     $db = Connect::connect(); 
     $account_id = $db->real_escape_string($this->account_id); 
     $cost = $db->real_escape_string($this->cost); 
     $query = "UPDATE accounts set tokens=tokens-$cost WHERE id=$account_id"; 
     $result = $db->query($query); 

    } 

} 

?> 

当我死了(“$查询”);直接在查询之后,它会打印正确的语句,并且当我在MySQL中运行该查询时,它可以很好地工作。

$ action = $ _REQUEST ['action'];

account.php只是一个命令列表,从来没有调用downloads.php。只是尝试评论重定向,但我有同样的问题。我不明白它是如何被调用两次,死亡语句显示正确的查询,并且脚本不重新加载自己。

这里是我的Apache访问日志:

71.*** - - [22/May/2010:13:14:35 +0000] "POST /download.php?action=confirm_download&photograph_id=122 HTTP/1.1" 200 1951 "http://***.com/viewphotograph.php?photograph_id=122" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)" 
71.*** - - [22/May/2010:13:14:36 +0000] "GET /download.php?action=place_order&photograph_id=122&size_radio=xsmall&unlimited=0 HTTP/1.1" 302 453 "http://*** .com/download.php?action=confirm_download&photograph_id=122" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)" 
71.*** - - [22/May/2010:13:14:36 +0000] "GET /download.php?action=place_order&photograph_id=122&size_radio=xsmall&unlimited=0 HTTP/1.1" 302 453 "http://*** .com/download.php?action=confirm_download&photograph_id=122" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)" 
71.*** - - [22/May/2010:13:14:36 +0000] "GET /account.php HTTP/1.1" 200 2626 "http://***.com/download.php?action=confirm_download&photograph_id=122" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)" 

据我所知,显然有些不对劲这里。但我无法弄清楚第二个请求来自哪里。

+0

调试总是比双眼好。准备运行的代码,不会被查看。 – 2010-05-22 12:04:18

+0

我一直试图运行这个代码四个小时。我已经睡了36个小时了。 – ThinkingInBits 2010-05-22 12:05:25

+0

如果我放了一个骰子(“$ query”);在结果之前,它显示正确的查询。如果我把它放在$ result之后,它仍然显示正确的查询,但错误的数量仍然会被扣除。 – ThinkingInBits 2010-05-22 12:07:29

回答

0

另一个远投,但在Firefox发生在我身上一次 - 翻了一番页的执行,导致了一倍插入和更新 - 关闭浏览器并重新启动。

0

我知道这听起来很奇怪,但要确保你没有在你的站点周围的地方有空的src =“”属性或任何指向空网址(如background:url();)的CSS样式标签当你有你的代码运行两次。

了解一些麻烦,这可能会导致此:http://hi.baidu.com/zhenyk/blog/item/38a1051fc63b96c3a686698f.html

+0

我有类似的问题,其中一个计数器每次请求更新两次,最后在幻灯片中出现JavaScript错误! – 2010-05-22 13:00:34

1

行:

header('location: account.php'); 

前锋的浏览器,但并没有结束PHP脚本
这可能没什么问题,你给出的代码片段不会在这行之后“做”任何事情。

另一种选择可能是一个双击
如果您提交按钮双击,表单将被发送两次。
我们在第一次点击后使用了一些javascript来禁用提交按钮。

+0

我已经尝试添加一个返回0;重定向后。我确保只点击一次。 – ThinkingInBits 2010-05-22 18:28:42