Welcome Guest, Not a member yet? Register   Sign In
UPDATE not work, PLZ Help me
#1

[eluser]Leftfield[/eluser]
Everybody Hello!
I did not clear the problem with the Update method, my simle method UPDATE does not work,and i have some problems with pass $id var into my update method , plz i need help of CI guru, thats my code:
Controller

Code:
function pageedit($id) {
        $this->load->library('common_lib');
        $this->common_lib->auth();
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');  
        $this->validation->set_rules('textfield', 'textfield', 'required');
        $this->validation->set_rules('textarea', 'textarea', 'required');    
        
//читаем запись из БД
        if (!$this->input->post('textfield') && !$this->input->post('textarea')) {
            $item = $this->Kernel->get($id);
            
            $this->validation->textfield = $item['title'];
            $this->validation->textarea = $item['fulltext'];
        }
        $data = array();
        $CI = &get;_instance();
        $title = $CI->config->item('admin_title');
        $author = $CI->config->item('admin_login');
        $data['title'] = $title;
        
        if ($this->validation->run() == FALSE) {
            $this->load->view('admin/editpage', $data);
        }
        else {
            
            $data = array(
                    'title' => $this->input->post('textfield'),
                    'fulltext' => $this->input->post('textarea'),
                    'date' => date('Y:m:d')
            );            
            $this->Kernel->update($data, $id);
            redirect('/admin/browsepages/');
        }
    }
Model
Code:
function update($data, $id) {
        $this->db->where('id', $id);
        $this->db->update('stand_alone_pages',$data);        
    }

View (Form)
Code:
<form id="form1" name="form1" method="post" action="/admin/pageedit/">
                                    <p>
                                        &lt;input name="textfield" type="text"   value="&lt;?php echo form_prep(@$this-&gt;validation->textfield)?&gt;" size="60" />
                                    </p>
                                    <p>
                                        &lt;textarea name="textarea"&gt;&lt;?php echo form_prep(@$this->validation->textarea)?&gt;&lt;/textarea&gt;
                                    </p>
                                    <p>
                                        &lt;input type="submit" name="Submit" value="Редактировать" /&gt;
                                    </p>
                                &lt;/form&gt;

Here is what I get


Quote:A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Admin::pageedit()

Filename: controllers/admin.php

Line Number: 95
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/admin.php

Line Number: 128
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at Z:\home\codecms\www\system\libraries\Exceptions.php:166)

Filename: helpers/url_helper.php

Line Number: 541

What am I doing wrong, tell me please ?
#2

[eluser]Buso[/eluser]
your method is expecting $id to come via GET, and you are sending it via POST

try fetching it like this:

Code:
$id = $this->input->post('id');

and remove $id from function pageedit($id)

anyway I can't see the id anywhere in the form

another option would be adding it to the form action uri
#3

[eluser]Leftfield[/eluser]
[quote author="Buso" date="1276373230"]your method is expecting $id to come via GET, and you are sending it via POST

try fetching it like this:

Code:
$id = $this->input->post('id');

and remove $id from function pageedit($id)

anyway I can't see the id anywhere in the form

another option would be adding it to the form action uri[/quote]

Plz say how to put it id into form and
Quote:another option would be adding it to the form action uri
if i remove
Quote:and remove $id from function pageedit($id)
i loose data in edit form page and i see many Notice like

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: controllers/admin.php

Line Number: 105
A PHP Error was encountered
thank you
#4

[eluser]Buso[/eluser]
this would be an example of sending id = 5 via url:
Code:
&lt;form id="form1" name="form1" method="get" action="/admin/pageedit/5"&gt;

but you probably want to make it a variable, so you should write something like:

Code:
&lt;form id="form1" name="form1" method="get" action="/admin/pageedit/&lt;?php echo $id; ?&gt;"&gt;

Another way could be adding a hidden field, with name='id' and value='5' for example, and receive its value via post with the rest of the form data
#5

[eluser]Leftfield[/eluser]
If i replace like you
Quote:&lt;form id="form1" name="form1" method="get" action="/admin/pageedit/&lt;?php echo $id; ?&gt;"&gt;

then i ricive on my admin/pageedit/54 action
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: id

Filename: admin/editpage.php

Line Number: 123
">
Quote:
And when i push the button Edit i ve got

Quote:An Error Was Encountered

The URI you submitted has disallowed characters.

and URL via
Quote:admin/pageedit/<div style=

Now my code look like:
Controller
Code:
function pageedit($id) {
        $this->load->library('common_lib');
        $this->common_lib->auth();
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        $this->validation->set_rules('textfield', 'textfield', 'required');
        $this->validation->set_rules('textarea', 'textarea', 'required');

//читаем запись из БД
        if (!$this->input->post('textfield') && !$this->input->post('textarea')) {
            $item = $this->Kernel->get($id);

            $this->validation->textfield = $item['title'];
            $this->validation->textarea = $item['fulltext'];
        }
        $data = array();
        $CI = &get;_instance();
        $title = $CI->config->item('admin_title');
        $author = $CI->config->item('admin_login');
        $data['title'] = $title;

        if ($this->validation->run() == FALSE) {
            $this->load->view('admin/editpage', $data);
        }
        else {

            $data = array(
                    'title' => $this->input->post('textfield'),
                    'fulltext' => $this->input->post('textarea'),
                    'date' => date('Y:m:d')
            );
            $id = $this->input->post('id');
            $this->Kernel->update($id, $data);
            redirect('/admin/browsepages/');
        }
    }

Model
Code:
function update($id, $data) {
        $this->db->where('id', $id);
        $this->db->update('stand_alone_pages',$data);
      
    }
View
Code:
&lt;form id="form1" name="form1" method="post" action="/admin/pageedit/&lt;?php echo $id ?&gt;"&gt;

                                    <p>
                                        &lt;input name="textfield" type="text"   value="&lt;?php echo form_prep(@$this-&gt;validation->textfield)?&gt;" size="60" />
                                    </p>
                                    <p>
                                        &lt;textarea name="textarea"&gt;&lt;?php echo form_prep(@$this->validation->textarea)?&gt;&lt;/textarea&gt;
                                    </p>
                                    <p>
                                        &lt;input type="submit" name="Submit" value="Редактировать" /&gt;
                                    </p>
                                &lt;/form&gt;

Plz can you show me right way, im gone insane Smile I really want to make it work
#6

[eluser]Buso[/eluser]
you have to pick an option, send the id via GET or POST

if you pick get, you have to put the id in the form action, and receive it as a parameter of the function

if you pick post, you have to put the id in a hidden field, and receive it using $this->input->post

for debugging you can set a default value for id, like this

Code:
function pageedit($id=NULL) {

  if($id===NULL) { print_r($_GET); print_r($_POST); }

}

maybe that way you can see what's going on
#7

[eluser]Matthieu Fauveau[/eluser]
You cannot pass variables to a controller method (except when they are used as internal functions inside the controller).

You just need to pass the $id of the db record to your view and post it via the form either via hidden field or via the action parameter. Then fetch it via $this->input->post (or ->get) and call your model function.
#8

[eluser]Buso[/eluser]
Huh? Yes you can, I do it all the time

http://ellislab.com/codeigniter/user-gui.../urls.html
#9

[eluser]Matthieu Fauveau[/eluser]
You missunderstood me Buso.

In is code he has a controller method
Code:
function pageedit($id) {

when it should be
Code:
function pageedit() {
$id = $this->input->post('id'); // or whatever
#10

[eluser]Buso[/eluser]
It can be the first! And actually it's the easiest way

Just write the id in the form action, like action='admin/pageedit/123', and '123' will be received as a parameter of the pageedit function




Theme © iAndrew 2016 - Forum software by © MyBB