【问题标题】:codeigniter: Update table from another table with sessioncodeigniter:使用会话从另一个表更新表
【发布时间】:2017-10-26 09:55:51
【问题描述】:

我在更新表格时遇到了一些挑战,表格与来自另一个表格的会话用户数据相结合。每次我运行时,它都会显示未定义的功能。请让我知道我在哪里弄错了,以便可以修复它。请抽出时间来查看它 我是新手。 这是控制器

 public function index()
    {

        if ( $this->session->userdata('logged_in') )
        {

            $id = $this->session->userdata('id');
            $this->load->model('Userinfo_model');
            $data['result'] = $this->Userinfo_model->get_users($id);

            $this->load->view("usermain_info", $data);

        }
    }
    public function update_user (){
        $data = $this->input->post('userId');
        $id = array(
            'balance' => $this->input->post('balance'),
            'id' => $this->input->post('id')
        );
        $this->Userinfo_model->update_user($id, $data);
        $this->index();
    }

型号

function get_users($userid)
    {

        if (isset($this->session->userdata['logged_in'])) {

            $userid = ($this->session->userdata['logged_in']['id']);
        } else {
            header("location: nwpgroup/nwp2/index.php");
        }

        /* all the queries relating to the data we want to retrieve will go in here. */

        $query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");


        return $query;
    }
    function update_user($id,$data){
        if (isset($this->session->userdata['logged_in'])) {

            $data = ($this->session->userdata['logged_in']['id']);
        } else {
            header("location: nwpgroup/nwp2/index.php");
        }
        $this->db->where('userId', $data);
        $this->db->update('walletbalance', $id);
    }

查看

<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
            <?php if($result->num_rows() == 0){
                echo 'No user found';
            }
            else {
                foreach ( $result->result_array() as $new_user ){ ?>
                    <h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
                    <h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
                    <h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
                    <input type="submit" id="submit" name="dsubmit" value="Update">
                <?php   }
            }
            ?>

错误信息

Message: Undefined property: Userinfo::$Userinfo_model

Message: Call to a member function update_user() on null

谢谢,谢谢

【问题讨论】:

  • 你能把错误代码贴在这里吗?
  • @MadAngle 我已经更新了
  • 在更新前尝试在模型中打印您的$id 数组。并且为了您的信息,无需将$data 发送给模型。 $this-&gt;Userinfo_model-&gt;update_user($id);足以调用模型函数
  • 你在Userinfo模型中扩展了Model类吗?

标签: php mysql database codeigniter


【解决方案1】:

如下改变你的观点:

<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
        <?php if($result->num_rows() == 0){
            echo 'No user found';
        }
        else {
            foreach ( $result->result_array() as $new_user ){ ?>
                <h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
                <h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
                <h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
                <input type="submit" id="submit" name="dsubmit" value="Update">
            <?php   }
        }
     ?>
</form>

只有当元素有名字时,表单才会向服务器发送数据

并且您不能多次提交表单。上面的代码将为每一行创建更新按钮。因此,如果您想在一次更新中更新所有记录,请在 codeigniter 中使用update_batch()。并更改视图如下:

foreach ( $result->result_array() as $new_user ){ ?>
                <h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
                <h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
                <h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>

            <?php   } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">

供参考:https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data

【讨论】:

    【解决方案2】:

    在您的 update_user 函数中,您没有包含 usermain_info 模型。这就是错误来的原因。 请创建构造函数并将模型包含在其中。我已经更新了你的代码。试试这个。

    <?php    
        function __construct() {
            parent::__construct();
            $this->load->library('session');
            $this->load->model('Userinfo_model');
        }
        public function index(){
            if ( $this->session->userdata('logged_in') ){
                $id = $this->session->userdata('id');            
                $data['result'] = $this->Userinfo_model->get_users($id);
                $this->load->view("usermain_info", $data);
            }
        }
        public function update_user (){
            $data = $this->input->post('userId');
            $id = array(
                'balance' => $this->input->post('balance'),
                'id' => $this->input->post('id')
            );
            $this->Userinfo_model->update_user($id, $data);
            $this->index();
        }
    
    ?>
    

    【讨论】:

    • 你好,我刚试了你的模型,更新后刷掉余额,返回0.00
    • 还是一样的结果,即返回 0.00
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多