【问题标题】:CodeIgniter Data insert is not workingCodeIgniter 数据插入不起作用
【发布时间】:2013-12-13 10:26:35
【问题描述】:

在 Codeigniter 项目中,当插入多个字段时,只有一个字段值没有插入到数据库中。

计费控制器代码

function billing()
{

    $arrInsert =array();
    $arrtoolsId =array();
    $this->db_session->unset_userdata('tools_cart');
    $arrtoolsId= $this->input->post('productId');
    foreach ($arrtoolsId as $val) {
    $arrToolrow = array();
    $productExclusivePrice =0;
    $arrToolrow =$this->product_model->getRow_products("gp.productId = '$val'");
    $price  =$arrToolrow['productPrice'];
    $actual_price=$this->input->post('actual_price');
    //$price    =$this->input->post('price');
    /////////////////////////////////////////////////////////////////////
    $qty=1;
        ////////////////////////////////////////////////////
    $variant ='';
    $total  = ($price * trim($qty));
    $engineNo=$arrToolrow['engineNo'];
    $vehicleClass               =$arrToolrow['vehicleClass'];
        $manufacturer               =$arrToolrow['manufacturer'];
        $model                      =$arrToolrow['model'];
        //Note variant
        $variant                    = $arrToolrow['hp'];
        $vin                        = $arrToolrow['vin'];
        $wcoNo                      = $arrToolrow['wcoNo'];
        $horsePower                 =$arrToolrow['horsePower'];
        $fuel                       =$arrToolrow['fuel'];
        $noCylinders            =$arrToolrow['noCylinders'];
        $manufactureDate            =$arrToolrow['manufactureDate'];
        $seatCapacity               =$arrToolrow['seatCapacity'];
        $unladenWeight              =$arrToolrow['unladenWeight'];
        $frontAxle                  =$arrToolrow['frontAxle'];
        $rearAxle                   =$arrToolrow['rearAxle'];
        $otherAxle                  =$arrToolrow['otherAxle'];
        $tandemAxle             =$arrToolrow['tandemAxle'];
        $color                      =$arrToolrow['color'];
        $keyNo                      =$arrToolrow['keyNo'];
        $grossVehicleWeight     =$arrToolrow['grossVehicleWeight'];
        $typeofBody             =$arrToolrow['typeofBody'];

    $productExclusivePrice=$arrToolrow['productExclusivePrice'];
    $productTax =  $arrToolrow['productTax'];
    $saleExclusivePrice = ($productExclusivePrice * trim($qty));
    $saleTax    = ($arrToolrow['productTaxamt'] * trim($qty));
    $saleCess   = ($arrToolrow['productCess'] * trim($qty));
        $arrInsert[]    =array (
                "productId" =>$val,
                "engineNo"=>$engineNo,
                "productPrice"  =>round($price,2),
                "productTax"    =>$productTax,
                "saleQty"   =>$qty,
                "salePrice" =>round($total,2),
                "saleTax"=>round($saleTax,2),
                "saleCess"=>round($saleCess,2),
                "saleExclusiveprice"=>round($saleExclusivePrice,2),
                "added_date" =>date("Y-m-d H:i:s"),
                "vehicleClass"              =>$vehicleClass,
                "manufacturer"              =>$manufacturer,
                "model"                     =>$model,
                "variant"                   =>$variant,
                "vin"                       =>$vin,
                "wcoNo"                     =>$wcoNo,
                "horsePower"                =>$horsePower,
                "fuel"                      =>$fuel,
                "noCylinders"               =>$noCylinders,
                "manufactureDate"           =>$manufactureDate,
                "seatCapacity"              =>$seatCapacity,
                "unladenWeight"             =>$unladenWeight,
                "frontAxle"                 =>$frontAxle,
                "rearAxle"                  =>$rearAxle,
                "otherAxle"                 =>$otherAxle,
                "tandemAxle"                =>$tandemAxle,
                "color"                     =>$color,
                "keyNo"                     =>$keyNo,
                "grossVehicleWeight"        =>$grossVehicleWeight,
                "typeofBody"                =>$typeofBody
                );
    }
    $this->db_session->set_userdata('tools_cart',$arrInsert);
    ///////////////////////////////////////////////////////////////////////////////
    $data['list_product']= $this->db_session->userdata('tools_cart');
    $this->load->view('header_login');
    $this->load->view('catalog/billing',$data);
    $this->load->view('footer_login');
}

账单控制器代码(点击销售页面的账单按钮后)

function bill()
{

    if($this->db_session->userdata('tools_cart')) {
    $data['list_product']= $this->db_session->userdata('tools_cart');                                                                               
    }else{
        redirect('catalog/sale');
    }       
    $salePrice = $this->input->post('price'); 
    $actual_price=$this->input->post('actual_price');

    ////////////////////////////////////////////////////////////CHANGE HERE
    $saleExclusiveprice = ($salePrice * 100) / 114.5;
    ///////////////////////////////////////////////////////////THIS CAN CHANGE EVERY YEAR            

    //$saleTax = $saleExclusiveprice *(14.5/100);

    //checking tax value with the current date...........
      $arrtax    = $this->tax_model->getTaxList(); 
        foreach($arrtax as $taxvalue) :       
          $currdate =$this->input->post('sale_date') ;       
          if(($taxvalue['fromdate']<=$currdate)&&($currdate<=$taxvalue['todate'])) 
          {
            // print_r($taxvalue['tax']); 
            $saleTax = $saleExclusiveprice *($taxvalue['tax']/100);
            $data['list_product'][0]['taxvalue'] = $taxvalue['tax'];                            
          }           
        endforeach;

    //$saleCess = $saleTax*(1/100);  //ORIGINALLY USED IN 2011
    $saleCess = 0;
    $data['list_product'][0]['salePrice'] = $salePrice;   
    $data['list_product'][0]['saleExclusiveprice'] = round($saleExclusiveprice,2);
    $data['list_product'][0]['saleTax'] = round($saleTax,2);
    $data['list_product'][0]['saleCess'] = round($saleCess,2);      
    $data['list_product'][0]['actual_price']=$this->input->post('actual_price');


        $scheme_id =serialize($arrtoolsId);
        $arrInsert  =array (
                "productId" =>$value['productId'],
                "saleCode"  =>$saleCode,
                "saleQty"   =>$value['saleQty'],
                "salePrice" => $value['salePrice'],
                "saleTax"   => $value['saleTax'],
                "saleCess"  => $value['saleCess'],
                "taxable_price" =>$taxable_price,
                "nontaxable_price" =>$nontaxable_price,
                "saleExclusiveprice"    => $value['saleExclusiveprice'],
                "saleCustomer"=>$saleCustomer,
                "saleUserId"=>$this->input->post('saleUserId'),
                "added_date" =>$date,
                "payType"=>$pay_type,
                "bank_id"=>$bank_id,
                "consultant_id"=>$consultant_id,
                "scheme_id"=>$scheme_id,
                "schemes_amount" => $total,
                "actual_price"  =>$actual_price

                );
        $saleId=0;
            if($this->product_model->insert_sale_entry($arrInsert)){
                    $saleId = $this->db->insert_id() ;
                    $arrStatus  =array (
                            "productSaleStatus" =>1
                                );
                $this->product_model->update_entry($arrStatus,$productId);
            }
    }
        $query  = $this->db->get_where('gm_users',array('userId'=>(int)$this->input->post('saleUserId')));
        if($query->num_rows >0) {
        $arrUser    =$query->row_array();
        }else{
            $arrUser    =array();     
        }   


    /*******************************************************************************/
    $this->db_session->unset_userdata('tools_cart');
    $data['arrtoolsId']     = $arrtoolsId;
    $data['saleCustomer']   =$saleCustomer;
    $data['saleCode']   =$saleCode;
    $data['saleId'] =$saleId;
    $data['saleDate']   =$date;
    $data['userinfo']   =$arrUser;
    $data['bank']       = $bank;
    $data['scheme']     = $schemeName;
    $data['wcoNo']      =$wcoNo;
    $data['actual_price'] =$actual_price;
    $this->load->view('header_login');
    $this->load->view('catalog/bill',$data);
    $this->load->view('footer_login');   
}

我尝试了很多方法,但都不起作用。我有 Dot net mvc 的实践经验,但它是我在 codeigniter 中的开始项目。我想知道这里的数据流是如何工作的以及如何解决这个问题?有我犯了什么逻辑错误?

型号代码

 function insert_sale_entry($data)
{
   if($this->db->insert('gm_sale', $data)){
     return true;
   }
  return false;
}

错误字段或未插入的值字段

$actual_price=$this->input->post('actual_price');

【问题讨论】:

  • 能不能去掉不相关的代码,只显示准确的代码
  • 在model中做数据库相关操作
  • @raheelshan 我已经从 bill() 中删除了不相关的代码

标签: php mysql codeigniter


【解决方案1】:

哪个字段没有被存储?你可以说得更详细点吗。该代码似乎一次存储一条记录,并且我认为只有一个从 post 变量调用的值是问题所在。如果您可以定义不存储的内容,将能够为您提供帮助

【讨论】:

  • $actual_price=$this->input->post('actual_price');
  • 上述字段 ie,"actual_price" 字段未更新。
  • 将变量定义替换为$this->input->post('actual_price');如果这不起作用,请检查该值是否存储在您维护的产品数组中。
  • 你好 Kunal Kinalekar,我在 db 中的实际值为“0”。但这里我的问题是,当我在实际价格字段中输入一个值时,它必须插入,但它对我不起作用..我必须为此做些什么.?
猜你喜欢
  • 2018-08-11
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 2014-08-07
  • 1970-01-01
相关资源
最近更新 更多