PHP:MySQL查询重复更新没有任何理由 [英] PHP: MySQL query duplicating update for no reason

查看:82
本文介绍了PHP:MySQL查询重复更新没有任何理由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是客户端代码,然后是类文件。



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

  if($动作=='place_order'){

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

$ license ='extended';

} else {

$ license ='standard';

}

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

$ token_cost = $ photo-> 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(请回去选择有效的照片大小);

}

}


###### 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);

}

}

?>

当我死($ query);直接在查询之后,它正在打印正确的语句,当我在MySQL中运行该查询时,它的工作正常。



$ action = $ _REQUEST ['action']; / p>

account.php只是一个订单列表,从来没有调用downloads.php。只是尝试注释重定向,但我有同样的问题。我不明白如何调用两次,die语句显示正确的查询,脚本不会自动重新加载。



这是我的apache访问日志:

  71。***  -   -  [22 / May / 2010:13:14:35 +0000] download.php?action = confirm_download& photos_id = 122 HTTP / 1.1200 1951http://***.com/viewphotograph.php?photograph_id = 122Mozilla / 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.1302 453http:// *** .com / download.php?action = confirm_download& photos_id = 122Mozilla / 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&unlimit ed = 0 HTTP / 1.1302 453http:// *** .com / download.php?action = confirm_download& photos_id = 122Mozilla / 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.1200 2626http://***.com/download.php?action = confirm_download& photos_id = 122Mozilla / 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)

我明白这里有明显的错误,但我无法确定第二个请求的来源。

解决方案

另一个长镜头,但是在Firefox中发生了一次 - 双倍的页面执行,导致插入和更新加倍 - 关闭浏览器并重新启动。


The code below is first the client code, then the class file.

For some reason the 'deductTokens()' method is calling twice, thus charging an account double. I've been programming all night, so I may just need a second pair of eyes:

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

    }

}

?>

When I die("$query"); directly after the query, it's printing the proper statement, and when I run that query within MySQL it works perfectly.

$action = $_REQUEST['action'];

account.php is just a list of orders, never does it call up downloads.php. Just tried commenting out the redirect, but I'm having the same problem. I don't understand how it's getting called twice, the die statements are showing the right query, and the script doesn't reload itself.

Here are my apache access logs:

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)"

I understand there's obviously something wrong here. But I can't figure out where the second request is coming from.

解决方案

Another long shot, but it happened to me once in Firefox - doubled page executions, resulting in doubled inserts and updates - close your browser and restart.

这篇关于PHP:MySQL查询重复更新没有任何理由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆