1

我正在尝试更新存储在数据库中的记录,但不幸的是,我遇到了如下错误(如下所示),请任何人帮助我,这对我很有帮助

遇到 PHP 错误

严重性:通知

消息:未定义的属性:stdClass::$id

文件名:views/display_about_us1.php

行号:44

回溯:

文件:C:\xampp\htdocs\CodeIgniter_try\application\views\display_about_us1.php 行:44

函数:_error_handler

文件:C:\xampp\htdocs\CodeIgniter_try\application\controllers\Home.php 行:107 功能:查看

文件:C:\xampp\htdocs\CodeIgniter_try\index.php 行:315 功能:require_once

控制器

public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Contact_model->displayrecordsById($id);
$this->load->view('update_about_us1',$result);  

if($this->input->post('update'))
    {
        // Basic validation
        $this->form_validation->set_rules('first_content', 'First content', 'required');
        $this->form_validation->set_rules('second_content', 'Second content', 'required');
        $this->form_validation->set_rules('third_content', 'Third content', 'required');
        $this->form_validation->set_rules('fourth_content', 'Fourth content', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('update_about_us1'); 
        }

        else
        {
            $id = $this->input->post('id');
            $data = array();
            $data['first_content']=$this->input->post('first_content');
            $data['second_content']=$this->input->post('second_content');
            $data['third_content']=$this->input->post('third_content');
            $data['fourth_content']=$this->input->post('fourth_content');
            $this->Contact_model->updaterecords($id, $data);
            redirect("Home/About_us_display");
        }
    }
}

模型

function displayrecordsById($id)
{
        $query=$this->db->query("select * from about_us1 where id='".$id."'");
        return $query->result();
}

function updaterecords($id,$data)
{
    $this->db->where('id', $id);
    $this->db->update('about_us1', $data);
}

看法

        <!DOCTYPE html>
            <html>
            <head>
            <title>About_us1 page</title>
            </head>

            <body>
             <?php
              $i=1;
              foreach($data as $row)
              {
              ?>
                <form action="Home/updatedata" method="get">
                    <table width="600" border="1" cellspacing="5" cellpadding="5">
              <tr>
                <td width="230">First content </td>
                <td width="329"><textarea rows="4" cols="50"  name="first_content" value="<?php echo $row->first_content; ?>"/></textarea></td>
                <span><?php echo form_error("first_content");?></span>
              </tr>
              <tr>
                <td>Second content </td>
                <td><textarea rows="4" cols="50" name="second_content" value="<?php echo $row->second_content; ?>"/></textarea></td>
                <span><?php echo form_error("second_content");?></span>
              </tr>
              <tr>
                <td>Third content </td>
                <td><textarea rows="4" cols="50" name="third_content" value="<?php echo $row->third_content; ?>"/></textarea></td>
                <span><?php echo form_error("third_content");?></span>
              </tr>
              <tr>
                <td>Fourth content </td>
                <td><textarea rows="4" cols="50" name="fourth_content" value="<?php echo $row->fourth_content; ?>"/></textarea></td>
                <span><?php echo form_error("fourth_content");?></span>
              </tr>
              <tr>
                <td colspan="2" align="center">
                <input type="submit" name="update" value="Update Records"/></td>
              </tr>
            </table>
                </form>
                <?php } ?>
            </body>
            </html>




            // display record view page 



            <!DOCTYPE html>
            <html lang="en">
                <head>
                    <title>display</title>
                    <meta charset="utf-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1">
                    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
                </head>

                <body>
                    <br><br>
                    <div class="container">

                        <div class="panel panel-primary">
                            <div class="panel-heading text-center">Contact Us Details</div>
                        </div>

                        <div class="table-responsive">          
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>First Content</th>
                                        <th>Second Content</th>
                                        <th>Third Content</th>
                                        <th>Fourth Content</th>
                                        <th>Update Content</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <?php
                                            $i=1;
                                            foreach($data as $row)
                                            {
                                                echo "<tr>";
                                                echo "<td>".$i."</td>";
                                                echo "<td>".$row->first_content."</td>";
                                                echo "<td>".$row->second_content."</td>";
                                                echo "<td>".$row->third_content."</td>";
                                                echo "<td>".$row->fourth_content."</td>";
                                                echo "<td><a href='updatedata?id=".$row->id."'>Update</a></td>";
                                                echo "</tr>";
                                                $i++;
                                            }
                                        ?>
                                    </tr>
                                </tbody>  
                            </table>
                        </div><br><br>

                        <a href="<?php echo site_url("Home/backend"); ?>" style="margin-left: 40%"> <button type="button" class="btn btn-success">Back</button></a>

                    </div>
                </body>
            </html>
4

1 回答 1

1

希望对你有帮助 :

updaterecords参数与带有模型的控制器中的方法不匹配

应该是这样的:

像这样在控制器中调用updaterecords方法:

$this->Contact_model->updaterecords($id, $data);

你的模型方法应该是这样的:

function updaterecords($id,$data,)
{
    $this->db->where('id', $id);
    $this->db->update('about_us1', $data);
}

更新 :

你的控制器代码应该是这样的:

if($this->input->post('update'))
{
    $data = array();
    $id = $this->input->post('id');
    $data['first_content']=$this->input->post('first_content');
    $data['second_content']=$this->input->post('second_content');
    $data['third_content']=$this->input->post('third_content');
    $data['fourth_content']=$this->input->post('fourth_content');
    $this->Contact_model->updaterecords($id, $data);
    redirect("Home/About_us_display");
}
于 2018-07-10T07:41:27.970 回答