在Codeigniter 3数据库中按ID更新1列的数据时出错

vqlkdk9b  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(92)

我正在发布托管订单,我想增加“托管”表中的“销售数量”列,但托管表中所有包的数据都在增加。
下面是我的相关代码,我可以说没有方法我没有试过。

$hostingdata = $this->db->query("select number_of_sales from hostings where id=" . $this->input->post('package_id'))->row();
                
                 $quantity = 1;
                 $new_number_of_sales = $hostingdata->number_of_sales + $quantity;
                 $data = array(
                     'number_of_sales' => $new_number_of_sales
                 );
                
                 $this->db->update('hostings', $data);

//完整代码

public function buy_hosting_post()
    {

        if ($this->input->post('package_id') && $this->session->userdata('id')) {
            $hosting = $this->db->from('hostings')->where('id', $this->input->post('package_id'))->get()->row_array();

            if ($hosting) {

                $data = array(
                    'user_id' => $this->session->userdata('id'),
                    'domain' => $this->input->post('domain'),
                    'price' => $this->input->post('price'),
                    // 'end_date' => date('Y-m-d h:i:s',$end_date), 
                    'package_id' => $hosting['id'],
                    'package_title' => $hosting['name'],
                    'payment_status' => 0,
                );

                $this->db->insert('hosting_orders', $data);

              
                $hostingdata = $this->db->query("select number_of_sales from hostings where id=" . $this->input->post('package_id'))->row();
                
                 $quantity = 1;
                 $new_number_of_sales = $hostingdata->number_of_sales + $quantity;
                 $data = array(
                     'number_of_sales' => $new_number_of_sales
                 );
                
                 $this->db->update('hostings', $data);

                $hosting_order_successful = array(
                    'hosting_order_successful' => 'success',
                );
                $this->session->set_userdata('hosting_order_successful', $hosting_order_successful);

                redirect(("hosting-siparisi-olusturuldu"));
            } else {
                redirect(base_url());
            }
        } else {
            redirect(base_url());
        }
    }
ha5z0ras

ha5z0ras1#

我用下面的方法解决了这个问题,但是它使系统变得笨重。

$hostingdata = $this->db->query("select number_of_sales from hostings where id=" . $this->input->post('package_id'))->row();

                $quantity = 1;
                $new_number_of_sales = $hostingdata->number_of_sales + $quantity;
                $data = array(
                    'number_of_sales' => $new_number_of_sales
                );
                $this->db->where('id', $this->input->post('package_id'));  // added here.
                $this->db->update('hostings', $data);

相关问题