【问题标题】:How to Update Two table Data one after other In codeigniter?如何在codeigniter中一个接一个地更新两个表数据?
【发布时间】:2015-07-30 07:36:24
【问题描述】:

如何使用 vendor_plan_task_status_mapp 表更新计划。

计划更新的模型是

public function updatePlanData(){

    $planId = $this->input->post('plan_id');
    $data = array(

                    'plan_title' => $this->input->post('plan_title'),
                    'plan_price' => $this->input->post('plan_price'),
                    'plan_desc'     => $this->input->post('plan_desc')

                );

    $this->db->where('plan_id', $planId);
    $this->db->update('tbl_plan', $data);


    $this->db->where('plan_id',$planId);
    $this->db->delete('plan_task_mapping');



    foreach ($this->input->post('task_id') as $key => $value)

     {
        $data2 = array(

        'plan_id' => $planId,
        'task_id'   => $value

        );
     // echo "Index {$key}'s value is {$value}.";
           $this->db->insert('plan_task_mapping', $data2);
     }

//-------- HEAR I NEED A CODE TO UPDATE The V_T_S_M table-----------

}

在第一个表更新后,我想更新 vendr_task_status_mapping 表中的数据??????

【问题讨论】:

  • 永远不要在 foreach 中插入、更新或删除。使用 insert_batch。
  • 好的,但是听到我更新了第一个表的数据然后我想要另一个表中的更新数据....先生我该怎么办???
  • @Deep Parekh 先生有什么帮助吗???
  • 你好,直到你上传到这里的代码你做对了吗?现在你想更新 V_T_S_M 表吗?
  • 先生,这个计划数据在供应商表中使用,所以这是更改供应商表得到错误...听到代码只更新 vedding_plan_task_status 现在我想更新 VTSM 表...

标签: php mysql codeigniter sql-update


【解决方案1】:

在此答案中,如果您更改计划但使用此代码您必须简单地编辑和更新它,并且此模型为您的任务创建一个新 ID 并再次插入它,您会收到错误。

 public function vendorUpdateModel($data)

       {
         $vendor_id = $this->input->post('vendor_id');
         $data = array(
           'category_id' => $this->input->post('category_id'),
           'plan_id' => $this->input->post('plan_id'),
           'city_id' => $this->input->post('city_id'),
           'business_name' => $this->input->post('business_name'),
           'owner_name' => $this->input->post('owner_name'),
           'contact_no1' => $this->input->post('contact_no1'),
           'contact_no2' => $this->input->post('contact_no2'),
           'vendor_email' => $this->input->post('vendor_email'),
           'subscription_date' => $this->input->post('subscription_date'),
           'vendor_description' => $this->input->post('vendor_description'),
           'vendor_address' => $this->input->post('vendor_address')

           );

       $this->db->where('vendor_id', $vendor_id);
       $this->db->update('vendor',$data);

       $this->db->where('vendor_id',$vendor_id);
       $this->db->delete('vendor_task_status_mapping');

       $this->db->select('task_mapp_id');
       $this->db->where('plan_id',$this->input->post('plan_id'));

       $query = $this->db->get('plan_task_mapping');

       foreach($query->result() as $row)

       {
       $data2 = array(

       'task_mapp_id' => $row->task_mapp_id,
       'vendor_id'  => $vendor_id,
          'task_status'  => '0'

          );

             $this->db->insert('vendor_task_status_mapping', $data2);
        }
          return;
         }

【讨论】:

  • 谢谢...它已经完成了..但是每次我更新计划时都会出错我如何根据计划任务自动更新供应商数据????
【解决方案2】:

如果您在 plan_task_mapping 表中添加了一个字段以添加唯一编号,那么...正如您提到的代码:

$unique=array();
foreach ($this->input->post('task_id') as $key => $value)
 {
   $no=rand(0, 15);
    $data2 = array(
    'unique_no'=>$no,
    'plan_id' => $planId,
    'task_id'   => $value
    );
   $unique[] = $no; //store no in array to use it.

 // echo "Index {$key}'s value is {$value}.";
       $this->db->insert('plan_task_mapping', $data2);
 }

在删除plan_task_mapping 表数据之前。获取该数据。

function select()
{
    $this->db->where('plan_id',$planId);
    $Q = $this->db->get('plan_task_mapping');
    if($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row)
        {
            $data[] = $row;
        }
    }
    $Q->free_result();
    return $data;
}

然后删除您在代码中提到的数据:

 $this->db->where('plan_id',$planId);
 $this->db->delete('plan_task_mapping');

然后从 VTSM 表中删除旧数据:

 $this->db->where('vendor_plan_task_mapping_id',$data[0]['id']); //this data[0]['id'] come form select(). change field name if required.
 $this->db->delete('VTSM');

这里通过唯一编号获取新插入的数据://我们将其存储在数组中。

foreach($unique as $u_no)
{
   $this->db->where('unique_no',$u_no);
    $Q = $this->db->get('plan_task_mapping');
    if($Q->num_rows() > 0)
    {
        foreach ($Q->result_array() as $row1)
        {
            $plan[] = $row1;
        }
    }
    $Q->free_result();
    return $plan;
}

在上面的代码中,我们获取了新插入的数据以获取它们的 id 以插入状态。

现在插入状态:

foreach($plan as $a)
{
    $Sdata=array(
        "plan_task_mapping_id"=>$a['id'], //this is new id change name if required
      "status"="your new status");
      $this->db->insert('VTSM',$Sdata);

}

【讨论】:

  • 不,先生,更新的 id 只更新单个数据,我在映射表中有许多 id 作为任务
  • 在循环插入新数据时,您必须在plan_task_mapping 表中插入一个唯一数字。这样我们就可以在需要时使用这个唯一编号。
  • 是的,先生,我解决了。我将代码放入供应商插入中。在插入供应商时,我会更新数据。如果我之前更改了我在供应商中选择的相同计划,我会遇到一些错误。所以我必须重新编辑并插入它。 !否则我做到了。
  • 先生,第一个答案是我所做的,但每次计划更改时我都必须编辑条目..你能帮我自动更新它吗?希望你明白我在说什么
  • 我为每个供应商使用计划任务.. 意味着每个供应商都有自己的计划,选择计划供应商会得到一些任务(计划任务)。现在当输入一些供应商选择了plan1(plan1 hase 2task)所以供应商得到2个plantask。现在如果我更新计划任务(我改变它就像我选择 3task 而不是两个 2 任务)所以在更新数据后他们会得到所有新的 id。我在供应商中使用了以前的 ID,所以我在供应商中有更新的 ID,但我没有它,所以我再次更新供应商数据,所以我在供应商任务映射表中获得最新的计划任务 ID。
【解决方案3】:

使用$this->db->insert_batch(); 而不是插入foreach loop。 看看这个link如何使用它。

【讨论】:

  • 我不能使用它,因为我想更新 2 个表,但在我的系统中我必须更新第一个表,然后我从该更新的表中获取数据并使用该数据更新另一个表。好的先生
  • 我使用更新后的表 ID,它可能不是单个实体,我知道如何获取最后插入的 ID,但听说我想要所有更新的 ID ???
猜你喜欢
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多