PHP错误遭遇codeigniter

时间:2016-05-07 08:45:44

标签: php codeigniter

我正在尝试从数据库中选择和更新数据。但是当我点击更新时,它会显示消息“Undefined variable:data”。关于$ data有什么问题。 ?我无法与testing3.php链接以更新我的数据。

//视图/ testing2.php

<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo anchor('testing3/view', 'Update'); ?></td>
</tr>   
<?php endforeach; ?>

//视图/ testing3.php

<html>
<h1>Update</h1>
<form method="post" >
<?php foreach ($data as $row): ?>
<input type="hidden" name="id" value ="<?php echo $row->id; ?>">
<label>ID: </label><br>

<input type="text" name="name" value ="<?php echo $row->name; ?>">
<label>ID: </label><br>

<input type="text" name="email" value ="<?php echo $row->email; ?>">
<label>ID: </label><br>

<input type="submit" id="submit" name="submit" value ="update">
<label>ID: </label><br>
<?php endforeach; ?>
</form>
</html>

//控制器/ testing3.php

<?php
//page name
class Testing3 extends CI_Controller  {

public function __construct()
{
    parent::__construct();
    $this->load->model('testing3_model');
}

function showid()
{
    $data['data']=$this->testing3_model->getsitemodel();
    $data['data']=$this->testing3_model->showid($id);
    $this->load->vars($data);
    $this->load->view('testing3', $data);
    $this->load->helper('html');
    $this->load->helper('url');
    $this->load->helper('form');
}

function updateid()
{

    $data=array(
    'id'=>$this->input->post('id'),
    'name'=>$this->input->post('name'),
    'name'=>$this->input->post('name')
    );
    $this->testing3_model->updateid($id, $data);
    $this->showid();
    redirect('testing');
 }



public function view($page = 'testing3') //about us page folder name
{

    if ( ! file_exists('application/views/testing3/'.$page.'.php')) //link
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = 'Testing3'; 
    //$data['title'] = ucfirst($page); // Capitalize the first letter
    $this->load->helper('html');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->view('templates/header', $data);
    $this->load->view('testing3/'.$page, $data);
    $this->load->view('templates/footer', $data);
}
}

//模型/ testing3_model.php

<?php
class Testing3_model extends CI_Model{

public function __construct()
{
    $this->load->database(); // to authorize connection to the database
}
//fetch record
public function getsitemodel()
{
    $data = $this->db->get('testing'); //pass data to query
    return $data->result();
}
//fetch selected record
public function showid($data)
{
    $this->db->select('*');
    $this->db->from('testing');
    $this->db->where('id', $data);
    $data = $this->db->get();
    return $data->result; 
}
//update record
public function updateid($id, $data)
{
    $this->db->where('id', $id); 
    $this->db->update('testing', $data); 
}

}
?>

1 个答案:

答案 0 :(得分:0)

改变这个:

function updateid()
{

    $data=array(
    'id'=>$this->input->post('id'),
    'name'=>$this->input->post('name'),
    'name'=>$this->input->post('name')
    );
    $this->testing3_model->updateid($id, $data);
    $this->showid();
    redirect('testing');
 }

到此:

function updateid()
{

    $data=array(
    'id'=>$this->input->post('id'),
    'name'=>$this->input->post('name'),
    'name'=>$this->input->post('name')
    );
    $this->db->where('id', $this->input->post('id'));
    $this->db->update('yourtablename', $data);

    redirect('testing');
 }