2016-05-07 5 views
-1

データベースからデータを選択して更新しようとしています。しかし、私が更新をクリックすると、 "Undefined variable:data"というメッセージが表示されます。 $ dataに関する問題は何ですか? ?私は私のデータを更新するためにtesting3.phpにリンクすることができません。PHPエラーが発生しましたcodeigniter

//views/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; ?> 

//views/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> 

//controllers/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); 
} 

} 
?> 
+0

[PHP: "注意:未定義の変数" と "注意:未定義のインデックス"]の可能な重複(http://stackoverflow.com/question/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Chris

+0

@Chrisこのようにhttp://stackoverflow.com/questions/4261133/php-noticeの重複は考えられませんOPがコントローラからビューに$ dataが渡されなかった理由を知りたいと思っているだけなので、-undefined-variable-and-notice-undefined-indexです。 – JiteshNK

+0

注:コントローラのファイル名はtesting3.phpでなくてはなりません。あなたのモデルにも同じことが適用されます。 – user4419336

答えて

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'); 
} 
関連する問題