-1

I am trying to select and update the data from database. But when I click on update, it show message "Undefined variable: data". What is the problem regarding $data. ? I am not able to link with testing3.php to update my data.

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

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

}
?>
David
  • 11
  • 1
  • 1
  • 4
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Chris May 07 '16 at 08:47
  • @Chris I don't think so this the possible duplication of http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index just because here OP wants to know why $data is been not passed from controller to view. – JiteshNK May 07 '16 at 08:52
  • Note: Your file name of the controller should be Testing3.php not testing3.php Same applies for your models. –  May 07 '16 at 08:55

1 Answers1

0

Change this:

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');
 }

to this:

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');
 }
Deniz B.
  • 2,532
  • 1
  • 18
  • 35