【问题标题】:How to make multiple requests MYSQL如何发出多个请求 MYSQL
【发布时间】:2012-02-29 12:39:39
【问题描述】:

用户想买30件,除了for循环还有其他方法吗?

我怎样才能做 30 次插入

public function buy($item_id)
{
    $price = $this->input->post('price');
    $amount = $this->input->post('amount');
    $sum = $this->input->post('sum');
    $price = intval($price);
    $amount = intval($amount);
    $sum = intval($sum);

    if(!is_numeric($price) || !is_numeric($amount) || !is_numeric($sum))
    {
        exit("Неверные значения");
        return false;
    }

    if(!empty($_SESSION['id']))
    {
        $user_id = $_SESSION['id'];
    }
    else
    {
        echo 'not logged';
        exit();
    }
    $q = $this->db->query("SELECT * FROM users WHERE id = ".$user_id);
    $t = $this->db->query("SELECT * FROM items WHERE id = ".$item_id." AND 
    TO_DAYS(end) - TO_DAYS(now()) >= 0");
    if($this->db->affected_rows() != 0)
    {
        $user = $q->result_array();
        $item = $t->result_array();
        $active = $user[0]['active'];
        $user_id = $user[0]['id'];
        $money = $user[0]['money'];
        $price = $item[0]['price'];
        $bought_times = $item[0]['bought_times'];
        $coupons = $item[0]['coupons'];
        $discount = $item[0]['discount'];
        $short_desc = $item[0]['short_desc'];
        $status = "";

        //echo $coupons;
        //exit();
        for($i = 0; $i < $amount; $i++)
        {
            if($coupons <= 0 || ($amount > $coupons))
            {
                $status = 'out of coupons';
                break;

            }
            if($money >= $price)
            {
                $money = $money - $price;
                $this->db->query("INSERT INTO user_items (`discount`, `short_desc`, `user_id`) 
                VALUES(".$discount.", '".$short_desc."', ".$user_id.")");
                $this->db->query("UPDATE users SET money = ".$money." WHERE id = ".$user_id);
                $coupons -= 1;
                $this->db->query("UPDATE items SET coupons = ".$coupons." WHERE id = ".$item_id);
                $q = $this->db->query("SELECT * FROM users WHERE id = ".$user_id);
                $user = $q->result_array();
                $money = $user[0]['money'];
                $_SESSION['money'] = $money;
                $status = 'buy successfull';
                //redirect('');
            }
            else
            {
                //
                $status = 'not enough money';
                break;

            }

        }
        switch($status)
        {
            case 'out of coupons':
            echo 'out of coupons';
            break;

            case 'buy successfull':
            $bought_times += 1;
            $this->db->query("UPDATE items SET bought_times = ".$bought_times." WHERE id = ".$item_id);
            echo 'buy successfull';
            break;

            case 'not enough money':
            echo 'not enough money';
            break;
        }
        /*foreach($q->result_array() as $u);
        {
            $_SESSION['id'] = $u[0]->id;
            $_SESSION['email'] = $u[0]->email;
            redirect('');

        }*/
        //return 'trolol';
    }
    else
    {
        echo 'out of time';
    }
    //echo $user_id;
}

这是 ajax 请求的控制器,在我的本地机器上一切正常,但在托管它给出了错误。

我需要减去钱和减去优惠券并插入行取决于用户数量

你能给我关于这段代码的建议吗?

【问题讨论】:

  • 换句话说,我的意思是如何优化这段代码???,我不知道如何使循环正确插入项目
  • 你为什么不使用 codeigniter 购物车类?

标签: php mysql ajax codeigniter


【解决方案1】:

让您的查询更具动态性,只需尝试使用可能会产生类似查询的循环进行串联即可:

INSERT INTO my_table(column1, column2, column3) VALUES ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3'), ('VAL1','VAL2', 'VAL3') ...

通过这种方式,您可以只抛出一个查询,但将多个项目插入到您的表中,只需用逗号 (,) 分隔每组值。

【讨论】:

  • 但是我怎样才能让它循环呢?我怎样才能在这些逗号之间放置循环?如果取决于数量
  • "但是我怎样才能让它循环呢?" => 使用 foreach 循环连接 sql 查询的值部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 2017-02-27
相关资源
最近更新 更多