简体   繁体   English

Codeigniter:一次将一行从一张表移到另一张表

[英]Codeigniter: Move one row from one table to another at one time

I'm kinda stuck here! 我有点卡在这里! I have to two tables free_members and domstic_members and I need to move selected rows from free_members tables to the domestic_members table. 我必须有两个表free_membersdomstic_members ,我需要将选定的行从free_members表移到domestic_members表。 why I'm saying selected rows because of I at the end of every row in free_members tables there is an upgrade option which will move that row to the domestic_members table. 为什么我要说所选行是因为在free_members表的每一行的末尾都有我,所以有一个升级选项,它将将该行移到domestic_members表中。 I have the code but it's not working the way I want. 我有代码,但是没有按我想要的方式工作。 right now what is happening is when I click on upgrade option it just copies the whole table records and sends it's to the other table. 现在发生的是,当我单击升级选项时,它只是复制整个表记录并将其发送到另一个表。 this here is the code 这是代码

Controller 调节器

function upgradeprofile() {
    //  $this->load->database();
    $this->load->model('freemember_model');

    $this->freemember_model->upgrade();
}

model 模型

function upgrade() {
    $query = $this->db->get('free_members');
    foreach ($query->result() as $row) {
        $this->db->where('id', $member_id);
        $this->db->insert('domestic_members', $row);
    }
}

View 视图

<center>  Search Member:
                    <input type="text" name ='search' id='search' placeholder="Search a member"/></center>
                    <input type="hidden" name ='limit_from' id='limit_from'/>
                    </form>
<br><center><button type="button" onclick="CallPrint1('background12')">Print</button>
                <div id="background12" class="box-body">
                  <table id="example1" class="table table-bordered table-striped">
                    <thead>
                      <tr>
                        <th>Sr#</th>
                        <th><center>Full Name</th>


                        <th><center>Mobile Number</th>
                         <th><center>Membership# / CNIC</th>
                        <th><center>Email Address</th>

                        <th><center>Province</th>
                        <th><center>District</th>
                        <th><center>Tehsil</th>
                        <th><center>Union Council</th>
<?php /*?>  <?php if($sessiondata['deletemembers']):?><?php */?>
     <?php if($sessiondata['username'] != 'bilal.sabir'):?>
                        <th><center>Delete</th>
  <?php endif;?>

    <?php /*?><?php if($sessiondata['data_modification'] && $sessiondata['username'] != 'bilal.sabir' ):?><?php */?>
         <?php if($sessiondata['username'] != 'bilal.sabir'):?>
                        <th><center>Print</th>
                        <th><center>Edit</th>
                        <th><center>Change Pwd</th>
                        <th><center>Upgrade</th>
    <?php endif;?>


                      </tr>
                    </thead>
                    <tbody>
                    <?php $sr=0;?>
                      <?php foreach ($freemembers as $member): ?>
<tr><td><center><?php echo $sr+=1; ?></td><td><center><?php echo $member->first_name; ?></td>
<td><center><?php echo $member->mobile; ?></td><td><center><?php echo $member->cnic; ?></td>
<td><center><?php echo $member->email; ?></td>
<td><center><?php echo $member->province; ?></td><td><center><?php echo $member->district; ?></td><td><center><?php echo $member->tehsil; ?></td><td><center><?php echo $member->uc; ?></td>
<?php /*?> <?php if($sessiondata['deletemembers']):?><?php */?>
     <?php if($sessiondata['username'] != 'bilal.sabir'):?>

<td><center><a href="<?php echo base_url() . "index.php/admin/deletefreemember/" . $member->id; ?>">delete</a></td>
<?php endif;?>
  <?php /*?><?php if($sessiondata['data_modification'] && $sessiondata['username'] != 'bilal.sabir' ):?><?php */?>
       <?php if($sessiondata['username'] != 'bilal.sabir'):?>
<td><center><a href="<?php echo base_url() . "index.php/admin/printcardfree/" . $member->id; ?>">print</a></td>

<td><center><a href="<?php echo base_url() . "index.php/admin/editprofilefree/" . $member->id; ?>">edit</a></td>
<td><center><a href="<?php echo base_url() . "index.php/admin/changepasswordfree/" . $member->id; ?>">change pwd</a></td>
<td><center><a href="<?php echo base_url() . "index.php/admin/upgradeprofile/" . $member->id; ?>">Upgrade</a></td>
<?php endif;?>
</tr>
<?php endforeach; ?>
                    </tbody>
                  </table>

this is the view of my table free_members 这是我的桌子的视图free_members

在此处输入图片说明

Based upon your View, you have this... 根据您的观点,您有...

index.php/admin/upgradeprofile/" . $member->id

So if $member->id = 100 the url will be 因此,如果$ member-> id = 100,则网址为

index.php/admin/upgradeprofile/100

So you are passing a member_id as mentioned in your model code BUT you are not getting it nor are you using the where correctly. 因此,您要传递模型代码中提到的member_id,但没有得到,也没有正确使用where。

So you need to get a handle on the member_id... 因此,您需要获取member_id的句柄...

This is ONE Solution/idea...This relies on you providing a correctly validated integer... You need to always Check/validate your inputs... 这是一个解决方案/想法...这取决于您提供正确验证的整数...您需要始终检查/验证输入...

Controller 调节器

function upgradeprofile($member_id) {
    // Need to check that $member_id is an integer
    // To be added by the OP

    $this->load->model('freemember_model');

    // $member_id is passed in via the URL.
    $this->freemember_model->upgrade($member_id); 
}

Model 模型

function upgrade() {
    // Which member do we want to get from the free_members Table?
    $this->db->where('id', $member_id);
    $query = $this->db->get('free_members');
    // Did we get a result? i.e was a valid member id passed in?
    if($query !== false) {
        $row = $query->row();
        $this->db->insert('domestic_members', $row);
    } else {
       // Do nothing or handle the case where an illegal number was used...
    }
}

Note: This code is not tested and is only to be used as a guide. 注意:此代码未经测试,仅供参考。 It can be refactored a number of ways but I am trying to keep it simple for now. 它可以通过多种方式进行重构,但我现在尝试使其简单。

So please, do not just copy and paste this without at least "thinking" about what you are trying to do and how the code might be written to achieve it. 因此,请不要在没有至少“思考”您要尝试做的事情以及如何编写代码以实现它的情况下复制和粘贴此内容。

The main points here are... 1. You have to use a member id of some kind so you can retrieve the record for the free member so you can insert it into the other table. 这里的要点是... 1.您必须使用某种会员ID,以便您可以检索免费会员的记录,以便可以将其插入另一个表中。 2. You are providing this "member id" in the url so you need to extract it. 2.您在URL中提供此“成员ID”,因此您需要提取它。 3. You can do that by either setting it as a parameter in the method being called... Or you can use segments ( look it up ). 3.您可以通过在被调用的方法中将其设置为参数来实现此目的,也可以使用句段(查找它)。

So once you have the member id you can fetch the free member entry and insert it into the other table BUT What do you want to happen to the entry in the free members table because that is still going to be there and now you have two copies of the same information... Delete it? 因此,一旦有了成员ID,就可以获取免费成员条目并将其插入到另一个表中,但是您想要对自由成员表中的条目进行什么操作,因为它仍然存在,现在您有两个副本相同的信息...删除吗?

If you need further explanation - ask. 如果您需要进一步的解释-请询问。

One last point. 最后一点。 You should be making sure you fully understand what is you are doing and not just doing it because someone said to or you thought it was a good idea... Have a Good Reason and think of the implications... 您应该确保您完全了解自己在做什么,而不仅仅是因为有人对您说过或者您认为这是个好主意而已。有充分的理由并思考其中的含义。

I'm not familiar with Codeigniter but MySQL syntax should be this: 我不熟悉Codeigniter,但MySQL语法应为:

INSERT INTO table1 (table1.row1, table1.row2) SELECT table2.row1, table2.row2 FROM table2 WHERE PUT_YOUR_CONDITION_IF_NEEDED

I've found this for Codeigniter. 我已经为Codeigniter找到了这个

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

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