I am a newbie with PHP and CodeIgniter. I am building a CRUD app that works well expect for the update operation.
My controller:
class Site extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function members_area()
{
$data = array();
if($query = $this->site_model->get_records()) // site model is autoloaded.
{
$data['records'] = $query;
}
$this->load->view('members_area', $data);
}
function create()
{
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->members_area();
}
function delete()
{
$this->site_model->delete_row();
$this->members_area();
}
function update()
{
$data = array(
'title' => $this->input->post('updtitle'),
'content' => $this->input->post('updcontent')
);
$this->site_model->update_record($data);
$this->members_area();
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
$this->load->view('login_form');
}
}
}
My model:
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('data');
return $query->result();
}
function add_record($data)
{
$this->db->insert('data', $data);
return;
}
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('data');
}
function update_record($data)
{
$this->db->where('id', $id);
$this->db->update('data', $data);
}
My view:
<h2>update</h2>
<?php echo form_open('site/update');?>
<p>
<label for="update title">Update Title:</label>
<input type="text" name="updtitle" id="updtitle" />
</p>
<p>
<label for="update content">Update Content:</label>
<input type="text" name="updcontent" id="updcontent" />
</p>
<p>
<input type="submit" value="Update" />
</p>
<?php echo form_close(); ?>
<hr />
If I attempt to update a record in my db, I get an error in my model ("Severity: Notice", "Message: Undefined variable: id") and the update does NOT happen.
What am I missing? Can you please help? Thank you.
I figured out a solution by re-working my app.
echo-ing $id and $data helped. Nothing was being output therefore nothing was being captured. Doing more reading got to redesign my app.
Now, I rely on a URI on the record I want to update:
1) in my controller, I added this update function
function update() {
$id = $this->uri->segment(3);
$title = $this->input->post('title');
$content = $this->input->post('content');
$this->posts_model->update_record($id, $title, $content)
}
2) in my model, I added this function
function update_record($id, $title, $content) {
$data = array(
'title' => $title,
'content' => $content
);
$this->db->where('id', $id);
$this->db->update('data', $data);
}
3) finally, I created a view just for the update
<?php $this->load->helper('form'); ?>
<?php echo form_open('site/update/'.$id); ?>
<?php echo form_input('title'); ?>
<?php echo form_textarea('content'); ?>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?
It works fine now. All I have to do is point my browser to site/update/post-id_number.
What I need to figure out is how to tailor the interface so I can get merge the update view with the other one I have.
Thanks for taking the time.
You didn't define $id
in your model call:
function update_record($data)
{
$this->db->where('id', $id);// <-- $id is not defined
$this->db->update('data', $data);
}
You aren't using it in your Controller call either:
function update()
{
$data = array(
'title' => $this->input->post('updtitle'),
'content' => $this->input->post('updcontent')
);
$this->site_model->update_record($data);
$this->members_area();
// Where is id??
}