我在CodeIgniter中有这个未定义的属性错误

时间:2016-12-21 18:37:40

标签: php codeigniter

我看到它在其他系统上运行。我无法理清。 同样的问题是last_name。此更新给出了问题。下面是它显示的错误,然后是整个代码。请帮忙。

  

严重性:注意

     

消息:未定义属性:mysqli :: $ first_name

     

文件名:views / main_view.php

     

行号:39

     

回溯:

     

文件:C:\ xampp \ htdocs \ test \ application \ views \ main_view.php行:39   功能:_error_handler

这是代码:

    <!DOCTYPE html>
<html>
<head>
    <title>View</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>



</head>
<body>


<form method="post" action="<?php echo base_url("main/form_validation")?>">

<?php

if($this->uri->segment(2)=="inserted")
{

    echo '<p> Data Inserted</p>';
}



  ?>

  <?php

if(isset($user_data))
{

    foreach ($user_data as $row) 
    {
        ?>



<div>
First Name:<input type="text" name="first_name" value="<?php echo $row->first_name; ?>"/>
<span class="text-danger"> <?php echo form_error("first_name");  ?></span> </br></br>
</div>



<div>
 Last Name:<input type="text" name="last_name" value="<?php echo $row->last_name; ?>">
 <span class="text-danger"> <?php echo form_error("last_name");  ?></span> </br></br>
</div>
<div>
<input type="hidden" name="hidden_id" value="<?php echo $row->id; ?>" >
<input type="submit"  name="update" value="Update" >

</div>
<?php


}
}else{




    ?>

<div>
First Name:<input type="text" name="first_name">
<span class="text-danger"> <?php echo form_error("first_name");  ?></span> </br></br>
</div>



<div>
 Last Name:<input type="text" name="last_name">
 <span class="text-danger"> <?php echo form_error("last_name");  ?></span> </br></br>
</div>



<div>
<input type="submit" name="hidde" value="Insert" >
<input type="hidden"  name="insert" value="Insert" >

</div>

<?php 
}  
?>
</form>
<br><br>

<div>
<table>
    <tr>
        <th>ID</th>
        <th> First Name</th>
        <th> Last Name </th>
        <th> Delete </th>
        <th> Update </th>



    </tr>

    <?php 

    if($fetch_data->num_rows()>0)
    {
        foreach ($fetch_data->result() as $row ) {
            ?>
            <tr>
                <td><?php echo $row->id;  ?> </td>
                <td><?php echo $row->first_name;  ?> </td>
                <td><?php echo $row->last_name;  ?> </td>
                <td> <a href="#" class="delete_data" id="<?php echo $row->id;  ?>"> Delete </a></td>
                <td> <a href=" <?php echo base_url(); ?>main/update_data/ <?php echo $row->id; ?>"> Edit  </a></td>



            </tr>


            <?php 
        }


    }else{
        ?>

        <tr>
            <th colspan="4">
                No data Found
            </th>

        </tr>


    <?php
    }


     ?>



</table>
</div>

<script type="text/javascript">


$(document).ready(function(){

    $('.delete_data').click(function(){

        var id = $(this).attr("id");
        if(confirm("Delete? or not?"))
        {
            window.location="<?php echo base_url(); ?>main/delete_data/"+id;

        }else{
            return false;


        }
    });
});

</script>



</body>
</html>

main_model.php

<?php



/**
* 
*/
class Main_model extends CI_Model
{

    function insert_data($data)
    {
        $this->db->insert("user",$data);
    }

    function fetch_data()
    {


        $query = $this->db->get("user");
        return $query;
    }


    function delete_data($id){

        $this->db->where("id", $id);
        $this->db->delete("user");

    }

    function fetch_single_data($id)
    {
        $this->db->where("id", $id);

        $query = $this->db->get("user");
        return $query;

    }

    function update_data($data,$id)
    {
        $this->db->where("id", $id);
        $this->db->update("user",$data);


    }
}


?>

main.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Main extends CI_Controller {




    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->model("main_model");
        $data["fetch_data"] = $this->main_model->fetch_data();

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

    public function form_validation()
    {



        $this->load->library('form_validation');
        $this->form_validation->set_rules("first_name","First Name", 'required|alpha');
        $this->form_validation->set_rules("last_name","Last Name", 'required|alpha');


        if($this->form_validation->run()){

            $this->load->model("main_model");

            $data = array('first_name' => $this->input->post("first_name"),
            'last_name' => $this->input->post("last_name") );

            if ($this->input->post("update")) {
                $this->main_model->update_data($data,$this->input->post("hidden_id"));

                redirect(base_url()."main/updated");
            }
            if ($this->input->post("insert")) {

                $this->main_model->insert_data($data);
                redirect(base_url()."main/inserted");
            }


    }else{

        $this->index();
    }
 }

public function inserted()
{

    $this->index();
}


public function delete_data(){


    $id= $this->uri->segment(3);
    $this->load->model("main_model");
    $this->main_model->delete_data($id);
    redirect(base_url()."main/deleted");

}


public function deleted()
{


    $this->index();
}

public function update_data()
{
    $user_id = $this->uri-> segment(3);
    $this->load->model("main_model"); 
    $data["user_data"]= $this->main_model->fetch_single_data($user_id);
    $data["fetch_data"] = $this->main_model->fetch_data();
    $this->load->view("main_view",$data);

}

public function updated()
{

    $this->index();
}
}



?>

1 个答案:

答案 0 :(得分:0)

模特试试:

function fetch_single_data($id)
{
    $this->db->where("id", $id);

    $query = $this->db->get("user")->result();
    return $query;

}