如何进行多个请求MYSQL

时间:2012-02-29 12:39:39

标签: php mysql ajax codeigniter

用户想购买let'say 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请求的控制器,在我的本地机器上所有的okey,但在托管中它给出了一个错误。

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

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

1 个答案:

答案 0 :(得分:1)

使您的查询更具动态性,只需尝试使用可能产生类似查询的循环进行连接:

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

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