CodeIgniter Forums
Basic 'Update Entry' Script help... - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Basic 'Update Entry' Script help... (/showthread.php?tid=29975)

Basic 'Update Entry' Script help... - El Forum - 04-28-2010


I'm working on a very basic Guestbook and I'm 90% of the way there.

However, I'd like to be able to allow Guests to edit posts. They can even edit others' posts, for now Smile

<title>Update Entry | Guestbook | CodeIgniter</title>

<style type="text/css">

body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 12px;
color: #4F5155;


<h1>Welcome to the Guestbook!</h1>

<p>Edit an entry on my Guestbook.</p>


echo validation_errors('<div class="error">', '</div>');

echo '<p>&nbsp;</p>';

echo form_open('guestbook/update/');

echo form_label('What is your Name?', 'author');
echo '<br />';
echo form_input('author', '');

echo '<br /><br />';

echo form_label('Your Comments', 'comments');
echo '<br />';
echo form_textarea('comments', '');

echo '<br /><br />';

echo form_submit('mysubmit', 'Update Entry!');




<p><br />Page rendered in {elapsed_time} seconds</p>


function update()
    $this->load->helper(array('form', 'url'));        
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        /* Form Validation */            
        $this->form_validation->set_rules('author', 'Author', 'trim|required');
        $this->form_validation->set_rules('comments', 'Comments', 'trim|required');  
        #Input and textarea field attributes
        $data["author"] = array('name' => 'author', 'id' => 'author');
        $data['comments'] = array('name' => 'comments', 'id' => 'comments');
        if ($this->form_validation->run() == FALSE)
          $data['post'] = $this->Guestbook_model->getEntry($id);
        $data['cats'] = $this->Guestbook_model->updateEntry();

function updateEntry(){
        $data = array('author' => $this->input->post('author'),
        'content' => $this->input->post('content')

I basically want to know how to:

1) pre-fill my input fields with the existing author/comments values for that post
2) update the guestbook entry when submitted correctly.

Many thanks for any help you can give.

Basic 'Update Entry' Script help... - El Forum - 04-28-2010

simply create an extraction db call like below

function edit_post()
$edit_post = $this->db->getwhere('guestbook', array('id' => $this->uri->segment(4)), '', '');
return $edit_post

the 4th uri segment should be the post ID number. however use this however you need it. if you use segment 3 than do that. Smile

$data['edit_post'] = $this->your_model->edit_post();

than do a foreach loop over the html &lt;form&gt;. and echo the results inside the value of the text box or textarea.

for example

&lt;?php foreach($edit_post->result() as $edit): ?&gt;
&lt;form action="your_form_action" method="post" enctype="multipart/form-data">
&lt;input type="text" class="field" name="somefield" value="&lt;?=$edit-&gt;somefield?&gt;" /><br />
&lt;?php endforeach;?&gt;

I noticed your using the ci form feature, but its not different just echo your field into the value="";

For update
function update_post()
    $this->db->where('id', $_POST['id']);
    $this->db->update('guestbook', $this->db->escape($_POST));