Welcome Guest, Not a member yet? Register   Sign In
Having trouble updating my database with CodeIgniter
#1

[eluser]cvandal[/eluser]
Hello,

Hopefully someone can help me out with this problem i'm facing trying to update my database. I'm trying to update my database with content specified in the form when I click on the Update link. The errors that I am receiving are below.

Quote:Undefined index: title
Undefined index: content
Cannot modify header information - headers already sent by (output started at /Users/cvandal/Sites/codeigniter/system/libraries/Exceptions.php:164)

The Model:
Code:
function update_entry($data)
    {
        $this->db->where('id');
        $this->db->update('pages', $data);
    }
The Controller:
Code:
function update_page()
    {
        $data = array(
            'title' => $_POST['title'],
            'content' => $_POST['content']
        );
        $this->welcome_model->update_entry($data);
        redirect('');
    }
The View:
Code:
<?=form_open('welcome/create_page');?>
            <p>
                <label for="title">Title:</label>
                &lt;input type="text" name="title" id="title" /&gt;
            </p>
            <p>
                <label for="content">Content:</label>
                &lt;textarea name="content" id="content"&gt;&lt;/textarea>
            </p>
            <p>
                &lt;input type="submit" value="Submit" /&gt;
            </p>
        &lt;/form&gt;
        &lt;?php if(isset($pages)):foreach($pages as $row): ?&gt;
            <h1>&lt;?=$row->title; ?&gt;</h1>
            <p>&lt;?=$row->content; ?&gt;</p>
            &lt;?=anchor("welcome/delete_page/$row->id", 'Delete'); ?&gt; - &lt;?=anchor("welcome/update_page/$row->id", 'Update'); ?&gt;
            &lt;?php endforeach; ?&gt;
            &lt;?php else: ?&gt;    
            <h1>You currently have no pages.</h1>
        &lt;?php endif; ?&gt;
#2

[eluser]Thorpe Obazee[/eluser]
You are submitting to 'welcome/create_page' on your form.

The controller method you are showing us is 'update_page'.

The Controller:
Code:
function update_page()
    {
        $data = array(
            'title' => $_POST['title'],
            'content' => $_POST['content']
        );
        $this->welcome_model->update_entry($data);
        redirect('');
    }
The View:
Code:
&lt;?=form_open('welcome/create_page');?&gt;
#3

[eluser]jegbagus[/eluser]
i'm sorry sir, might be you forget the ID parameter?
[quote author="cvandal" date="1252656053"]
The Model:
Code:
function update_entry($data)
    {
        $this->db->where('id');
        $this->db->update('pages', $data);
    }
[/quote]
#4

[eluser]cvandal[/eluser]
I want to use the same form to create and update content.

I've updated the form as follows:
Code:
&lt;?=form_open('welcome/create_page', 'welcome/update_page');?&gt;

I no longer get the "Cannot modify header information" error, but I still get the "Undefined index: title" and "Undefined index: content".

I have also added the following ID paramter:
Code:
$this->db->where('id', $this->uri->segment(3));
#5

[eluser]jegbagus[/eluser]
i'm not too sure, but might be you should check your $_POST parameter (null?).
why not to use CI input library instead use POST variable?
regards
#6

[eluser]cvandal[/eluser]
OK,

Thanks for the help guys, it's been very useful. I've been playing around with everything and this is what I have so far. No errors anymore but when I click on 'Update' it updates with NULL.

How can I get the anchor to read from the title and content fields in the form? I can't use:
Code:
&lt;?=form_open('welcome/create_page', 'welcome/update_page');?&gt;

The Model:
Code:
function update_entry($data)
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->update('pages', $data);
    }

The Controller:
Code:
function update_page()
    {
        $data = array(
            'title' => $this->input->post('title'),
            'content' => $this->input->post('content')
        );
        $this->welcome_model->update_entry($data);
        redirect('');
    }

The View:
Code:
&lt;?=form_open('welcome/create_page');?&gt;
            <p>
                <label for="title">Title:</label>
                &lt;input type="text" name="title" id="title" /&gt;
            </p>
            <p>
                <label for="content">Content:</label>
                &lt;textarea name="content" id="content"&gt;&lt;/textarea>
            </p>
            <p>
                &lt;input type="submit" value="Submit" /&gt;
            </p>
        &lt;/form&gt;
        &lt;?php if(isset($pages)):foreach($pages as $row): ?&gt;
            <h1>&lt;?=$row->title; ?&gt;</h1>
            <p>&lt;?=$row->content; ?&gt;</p>
            &lt;?=anchor("welcome/delete_page/$row->id", 'Delete'); ?&gt; - &lt;?=anchor("welcome/update_page/$row->id", 'Update'); ?&gt;
            &lt;?php endforeach; ?&gt;
            &lt;?php else: ?&gt;    
            <h1>You currently have no pages.</h1>
        &lt;?php endif; ?&gt;
#7

[eluser]Thorpe Obazee[/eluser]
I'm not sure if you got what I am trying to say.

You are not sending anything to your controller method. You are sending to the wrong controller method.

And you will probably better off using another form for updating.
#8

[eluser]cvandal[/eluser]
Sorry mate, you'll have to forgive me. I'm new to php and even more so to CodeIgniter.

If I use a separate form to update data, how will the form know which row to update? I was using the following in an achor but I'm not sure how I would go about it with a form and submit.

Code:
&lt;?=anchor("welcome/update_page/$row->id", 'Update'); ?&gt;
#9

[eluser]Thorpe Obazee[/eluser]
You could create a hidden form input in the form. You could also do it by using this form helper.

Code:
&lt;?php echo form_open('welcome/update_page/' . $row->id);?&gt;
#10

[eluser]cvandal[/eluser]
OK,

I've created a new view with a separate form for updating. I've used the form helper to select what information is loaded into the form when you click on the 'Update' link.

When the page loads, I get 2 errors, 'Undefined variable: title' and 'Undefined variable: content'.

I thought the variables would be loaded by these functions in my controller when it performs a query:
Code:
function update()
        {
            $data = array();
            if($query = $this->crud_model->get_entry())
            {
                $data['pages'] = $query;
            }
            $this->load->view('update_view', $data);
        }
        function update_page()
        {
            $data = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content')
            );
            $this->crud_model->update_entry($data);
            redirect('');
        }

The Model:
Code:
function update_entry($data)
        {
            $this->db->update('pages', $data);
            return $data->row();
        }

The view which contains the option to update:
Code:
<p>&lt;?=anchor("crud/update/$row->id", 'Update'); ?&gt;</p>

The view which contains the form which should be automatically populated when the user clicks on the 'Update' link in the previouse view:
Code:
&lt;?=form_open('crud/update_page') ?&gt;
            <p>
                <label for="title">Title:</label>
                &lt;input type="text" name="title" id="title" value="&lt;?=$title?&gt;" /&gt;
            </p>
            <p>
                <label for="content">Content:</label>
                &lt;textarea name="content" id="content"&gt;&lt;?=$content?&gt;&lt;/textarea&gt;
            </p>
            <p>
                &lt;input type="submit" value="Update" /&gt;
            </p>
        &lt;/form&gt;




Theme © iAndrew 2016 - Forum software by © MyBB