Frage

I have a weird problem with Codeigniter and Datamapper,

I am making an CMS system and I am trying to edit an article from the Database using Datamapper. When I click to edit the article I get the message 'You have succesfully saved the article ' without clicking on the SAVE button, also all the data becomes empty and leaves 0. When I enter the data and click on SAVE it saves it but again when I try edit it again it goes back to 0 ... Can someone help out please

Here is my edit function in the controller

public function edit($id = NULL)
{
   // Get articles by ID
    $articles = new Article_model();
    $article = $articles->where('id', $id)->get();

    if ($id)
    {
        $id == NULL || $article;
        count($article) || $error = 'Page not found ';
    }
    else
    {
        $article = $this->article_model->get_new();
    }

    $article->title = $this->input->post('title');
    $article->text = $this->input->post('text');

    if ($article->save())
    {
        echo 'You have succesfully saved the article';
    }
    else
    {
        echo 'Sorry something went terribly worng';
    }


    $data = array(
        'admin_content' => 'admin/article/edit',
        'article' => $article,
        );

    $this->parser->parse('admin/template_admin', $data);
}

and here is my view

<?php if ($this->tank_auth->is_logged_in()): ?>
<div class="container">
<div class="row">
    <div class="col-md-9">
      <h3><?php echo empty($article->id) ? 'Add an article' : 'Edit a Page ' . $article->title ;?></h3>

 <?php echo form_open(); ?>
<table class="table">
    <tr>
        <td>Publication Date</td>
        <td><?php echo form_input('pubdate', set_value('pubdate', $article->pubdate), 'class="datepicker"'); ?></td>
    </tr>
    <tr>
        <td>Title</td>
        <td><?php echo form_input('title', set_value('title', $article->title)); ?></td>
    </tr>
    <tr>
        <td>Body</td>
        <td><?php echo form_textarea('text', set_value('text' , $article->text), 'class="tinymce"'); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
    </tr>       
</table>

<?php echo form_close(); ?>
    </div>
</div>
</div>
<?php else: redirect('/auth/login'); ?>
<?php endif; ?>
War es hilfreich?

Lösung

that's not weird at all...every time that controller is loaded it's going to execute the save() method of the article class.

you should probably include some form validation and only save the article if the form was submitted and validation passes.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top