php - MYSQL如何发出多个请求

标签 php mysql ajax codeigniter

用户想购买 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请求的 Controller ,在我的本地机器中一切正常,但在托管中它给出错误。

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

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

最佳答案

使您的查询更加动态,只有一次查询才尝试使用循环进行串联,这可能会产生如下查询:

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

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

关于php - MYSQL如何发出多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499329/

相关文章:

php - 如何将文本发送到服务器并对其进行操作

javascript - 无法编辑从 ajax 调用收到的 PHP 数据

javascript - PHP 是否比 Nginx/Apache 更快地检查文件是否存在?

php - IE 所需的 CSS/Javascript 下拉导航修复

php - 替代 mcrypt_encrypt?

python - 如何在 Google 表格和 Mysql 数据库之间同步数据?

php - 我可以在 quote->getAllItems() 上调用哪些方法?

java - 生成唯一的客户 ID/在配置单元中插入唯一的行

mysql - 从多个表 MySQL 中搜索 SQL 查询

jquery - 如何提供一个使用 ASP.NET MVC 和 jQuery(不是自动完成)实时过滤结果的文本框?