如何使用CodeIgniter插入多个动态数据?

时间:2019-07-02 12:20:23

标签: php html codeigniter-3

我正在使用CodeIgniter,正在动态显示字段。现在,我必须将数据插入数据库中。所以我尝试了下面的代码。

$order = $this->input->post('order[]');
$partner = $this->input->post('parner[]');
$bankname = $this->input->post('newpartner[]');
$status = $this->input->post('filestatus[]');
$user_id = $this->input->post('user_id');

$order_length = sizeof($order);

for ($j = 0; $j < $order_length; $j++) {

  if (($status = 1) || ($status = 3)) {
    $remark = $this->input->post('remark[]');
  } else {
    $remark = "";
  }

  if (($status = 2) || ($status = 4))) {
  $reasonDate = $this->input-> post('reasonDate[]');
  $remark = $this->input-> post('remark[]');
} else {
  $reasonDate = "";
  $remark = "";

}

if ($status = 7) {
  $reasonAmt = $this->input->post('reasonAmt[]');
  $reason = $this->input->post('reason[]');
} else {
  $reasonAmt = "";
  $reason = "";
}


$data['row'] = array(
  'order' => $order[$j],
  'bankname' => $bankname[$j],
  'status' => $status[$j],
  'lead_id' => $user_id,
  'remark' => $remark[$j],
  'reasonDate' => $reasonDate[$j],
  'reasonAmt' => $reasonAmt[$j],
  'reason' => $reason[$j]
);
$save = array(
  'b_orderno' => $data['row']['order'],
  'b_bankname' => $data['row']['bankname'],
  'b_filestatus' => $data['row']['status'],
  'p_id' => $data['row']['pid'],
  'lead_id' => $data['row']['lead_id'],
  'b_remark' => $data['row']['remark'],
  'b_date' => $data['row']['reasonDate'],
  'b_amt' => $data['row']['reasonAmt'],
  'b_reason' => $data['row']['reason']
);

$afterxss = $this->security-> xss_clean($save);
if ($afterxss) {
  $this - > db - > insert('tbl_bankdata', $afterxss);
  $response['error'] = "true";
  $response['msg'] = "added successfully";

} else {
  $response['error'] = "false";
  $response['msg'] = "Sometning wrong! please check the internet connection and try again";
}

}
echo json_encode($response);

我在状态字段上遇到了问题,因为将显示取决于状态值的输入字段。另外,我在状态字段的逻辑中使用了条件条件。每行都有一个唯一的状态字段。

您将在下面的链接中找到我的HTML和脚本。

https://jsfiddle.net/08phzue3/

这是我的UI屏幕截图。忽略仅用于测试目的的值。

1)载入后将显示enter image description here 2)如果用户选择状态 enter image description here 3)如果多排 enter image description here

您能帮我吗?

1 个答案:

答案 0 :(得分:0)

实际上您不是在这里进行比较:

if (($status = 1) || ($status = 3)) {

您正在将值分配给$status变量

这应该是:

if (($status == 1) || ($status == 3)) {

旁注::在其他情况下也是如此。

编辑:

正如您提到的,您正在$status中获得数组,因此在这里使用比较不是正确的逻辑,有多种解决方案可用,

您可以像这样使用in_array()

if(in_array(1, $status) || in_array(3, $status)){
    $remark = $this->input->post('remark[]');   
}
else{
    $remark = "";   
}

最终解决方案(04-07-2019):

更改$save$data数组后,OP的问题已解决:

$save['b_orderno'] = $order[$j]; 
$save['b_bankname'] = $bankname[$j]; 
$save['b_filestatus'] = $status[$j]; 
$save['p_id'] = $partner[$j]; 
$save['lead_id'] = $user_id; 


if(!empty($remark[$j])){ 
$save['b_remark'] = $remark[$j]; 
} 
if(!empty($reasonDate[$j])){ 
$save['b_date'] = $reasonDate[$j]; 
} 
if(!empty($message[$j])){ 
$save['b_message'] = $message[$j]; 
} 
if(!empty($reasonAmt[$j])){ 
$save['b_amt'] = $reasonAmt[$j]; 
} 
if(!empty($reason[$j])){ 
$save['b_reason'] = $reason[$j]; 
}


$data['row']['order'] = $order[$j]; 
$data['row']['bankname'] = $bankname[$j]; 
$data['row']['status'] = $status[$j]; 
$data['row']['lead_id'] = $user_id; 
$data['row']['remark'] = $remark[$j]; 
$data['row']['reasonDate'] = $reasonDate[$j]; 

if(!empty($reasonAmt[$j])){ 
$data['row']['reasonAmt'] = $reasonAmt[$j]; 
} 
if(!empty($reason[$j])){ 
$data['row']['reason'] = $reason[$j]; 
}