使用CodeIgniter更新MySQL表中的行。

时间:2021-09-13 00:11:11

So I'm new to PHP and MySQL generally, and MVC applications. Forgive me if this is a question that has been answered already, I couldn't find anything that was crystal clear and showed the complete MVC flow of this.

我对PHP MySQL和MVC应用程序都不熟悉。如果这是一个已经回答过的问题,请原谅我,我找不到任何非常清楚的内容,并显示了完整的MVC流程。

So I have an HTML table full of inputs populated by data in a, I generate a form for each row as a separate table, and a submit button for each. I want the update button to update only the row associated with the generated form.

因此,我有一个HTML表,其中充满了数据填充的输入,我为每一行生成一个表单作为一个单独的表,并为每一行生成一个submit按钮。我希望update按钮只更新与生成的表单相关联的行。

If remove this->db->where('id', $id); from the model it updates all rows in the table with the same info, with it present nothing is updated.

如果删除这个- > db - >(id,id美元);从模型中,它更新表中的所有行,并提供相同的信息,但没有更新任何内容。

Any help or an alternative method for achieving this would be most appreciated. The controller function is also not pretty bad I think.

任何帮助或另一种实现这一目标的方法都是最值得赞赏的。我认为控制器的功能也不坏。

Thank you.

谢谢你!

VIEW

视图

<?php foreach($log->result() as $row): ?>
<?=form_open('hq/update_entry');?>
    <table class="editLog">
        <tr>
            <td><?php 
                $data = array(
                    'name' => 'no',
                    'value' => $row->no
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'date',
                    'value' => $row->date
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'artist',
                    'value' => $row->artist
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'title',
                    'value' => $row->title
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'long',
                    'value' => $row->long
                );
                echo form_input($data);
                ?></td>
            <td><?php 
                $data = array(
                    'name' => 'lat',
                    'value' => $row->lat
                );
                echo form_input($data);
                ?></td>
                <td>
            <?php
                echo form_hidden('id', $row->id);
                $data = array(
                    'class' => 'updateSubmit',
                    'value' => '✚'
                );
                echo form_submit($data);
            ?></td>
        </tr>
    </table>
<?=form_close();?>

CONTROLLER

控制器

function update_entry()
{
    $this->load->model('update_entry_model');

    if($query = $this->update_entry_model->update())
    {
        $this->index();
    }
    else
    {
        redirect('hq');
    }
}

MODEL

模型

<?php
    class Update_entry_model extends CI_Model {
        function update()
        {
            $data = array(
                'no' => $this->input->post('no'),
                'date' => $this->input->post('date'),
                'artist' => $this->input->post('artist'),
                'title' => $this->input->post('title'),
                'long' => $this->input->post('long'),
                'lat' => $this->input->post('lat')
            );
            $this->db->where('id', $id);
            $this->db->update('entries', $data);
        }
    }

1 个解决方案

#1


3  

Without doing further investigation, I see that in the model function update(), you are missing

没有进一步的调查,我看到在模型函数update()中,您丢失了。

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

The reason why adding this->db->where('id', $id); doesn't update anything is because $id is not currently set to any value.

添加这个->db->的原因是('id', $id);不更新任何东西是因为$id当前没有设置为任何值。

#1


3  

Without doing further investigation, I see that in the model function update(), you are missing

没有进一步的调查,我看到在模型函数update()中,您丢失了。

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

The reason why adding this->db->where('id', $id); doesn't update anything is because $id is not currently set to any value.

添加这个->db->的原因是('id', $id);不更新任何东西是因为$id当前没有设置为任何值。