简体   繁体   English

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

[英]How to insert multiple dynamic data using CodeIgniter?

I am using CodeIgniter, I am displaying the fields dynamically. 我正在使用CodeIgniter,正在动态显示字段。 Now I have to insert the data in the database. 现在,我必须将数据插入数据库中。 So I tried below code. 所以我尝试了下面的代码。

$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);

I am getting the issue on the status field because depending upon the status value input field will display. 我在状态字段上遇到问题,因为将显示状态值输入字段。 Also, I used If the condition in logic for the status field. 另外,我在状态字段的逻辑中使用了条件条件。 Each row has a unique status field. 每行都有一个唯一的状态字段。

You will find my HTML and script in below link. 您将在下面的链接中找到我的HTML和脚本。

https://jsfiddle.net/08phzue3/ https://jsfiddle.net/08phzue3/

This is my UI screenshot. 这是我的UI屏幕截图。 Ignore the value that is only for testing purpose. 忽略仅用于测试目的的值。

1)Onload this will display 1)加载时将显示 在此处输入图片说明 2) If user select the status 2)如果用户选择状态 在此处输入图片说明 3) If multiple row 3)如果多排 在此处输入图片说明

Would you help me out with this? 你能帮我这个忙吗?

Actually you are not comparing here: 实际上,您在这里没有进行比较:

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

You are assigning the values to your $status variable 您正在将值分配给$status变量

this should be: 这应该是:

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

Side note: same for other conditions as well. 旁注:在其他情况下也是如此。

Edit: 编辑:

As you mentioned you are getting array in $status so using comparison is not a correct logic here, there are multiple solution available, 正如您提到的,您将在$status中获得数组,因此在此处使用比较不是正确的逻辑,有多种解决方案可用,

You can use in_array() like: 您可以像这样使用in_array()

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

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

After changing the $save and $data arrays, issue has been resolved for OP: 更改$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]; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM