简体   繁体   中英

why is my update query creating new row?

I'm using codeigniter and trying to do an UPDATE query, but it keeps creating a new row like an INSERT query. I'm baffled and searching the web didn't yield much for me. (I'm a bit of a n00b). Any help would be greatly appreciated.

Here is the form:

<form action="<?=base_url();?>blog/new_post" method="post" name="write_new">
    <input type="hidden" name="user" value="<?=$user?>">
    <label>Title:</label><br>
    <input type="text" name="title" value="<?php if(isset($query->title)) { echo $query->title; }?>" size="50"><br>
    <label>Date:</label><br>
    <input type="text" name="date" size="46" value="<?php if(isset($query->date)) { echo $query->date; }?>" /> 
    <script language="JavaScript"> 
    new tcal ({
        // javascript calender thingy
        // form name
        'formname': 'write_new',
        // input name
        'controlname': 'date'
    });
    </script> <br><br>
    <label>Article:</label><br>
    <textarea name="content" style="width:100%;height:300px;"><?php if(isset($query->article)) { echo $query->article; }?></textarea><br>
    <select name="category">
        <option value="news">Dreamsy News</option>
        <option value="web">From the Interwebs</option>
    </select>
    <input type="submit"/>
</form>
<div class="clearboth"></div>

Here's the model:

function edit_post($title, $date, $author, $article, $category, $id)
    {
        $this->load->helper('form'); //loads the CI form helper
        $this->load->library('form_validation'); //loads the form validation class
        $this->form_validation->set_rules('title', 'title', 'required|min_length[3]|max_length[40]|xss_clean'); 
        $this->form_validation->set_rules('date', 'date', 'required|min_length[5]|max_length[15]|xss_clean'); 
        $this->form_validation->set_rules('category', 'category', 'required|xss_clean'); 
        $this->form_validation->set_rules('content', 'article', 'required|xss_clean'); 
        $this->form_validation->set_rules('user', 'you are not logged in', 'required'); 

        if ($this->form_validation->run() == FALSE) //If the form does not validate
        {       
            $this->load->vars(array('title' => 'Error'));
            $this->template->write_view('content', 'error/new_post_fail');
            $this->template->render();
        }
        else
        {

            $article = htmlentities($article, ENT_QUOTES);          

            /*$data = array(
               'title' => $title,
               'date' => $date,
               'author' => $author,
               'article' => $article,
               'category' => $category
            );*/

            //$this->db->where('id', $id);
            //$this->db->update('blog', $data);

            $query = 'UPDATE blog SET title = '.$title.' date = '.$date.', author = '.$author.', article = '.$article.', category = '.$category;

            $this->db->query($query);

            redirect(base_url().'blog');
        }

And here's the controller:

public function write()
{
    $id = $this->uri->segment(3);
    $query = 'SELECT * FROM blog WHERE id = '.$id;
    $query = $this->db->query($query);
    $query = $query->row();
    $title = $query->title;
    $user = $this->session->userdata('user_id');
    if(isset($id)){ $title = 'Edit: '.$title; } else { $title = 'Write new post'; }

    $vars['title'] = $title;
    $vars['page'] = 'blog';
    $vars['user'] = $user;
    $vars['id'] = $id;
    $vars['query'] = $query;

    $this->load->helper('form'); //loads the CI form helper
    $this->load->library('form_validation'); //loads the form validation class
    $this->load->vars($vars); 
    $this->template->write_view('content', 'blog/write');
    $this->template->render();
}
<form action="<?=base_url();?>blog/new_post" method="post" name="write_new">

You have to modify the action attribute to call the edit method of your blog controller. Otherwise the new_post method is called, which as far as I can see just inserts the POST data.

Did you set up some route, because i didnt see related function in your above controller which referencing by action tag/attr from your form. It hard to guest where the error came from, if you doesn't include that function.

Some highlight from me, i notice you put form validation, template stuff and even redirection function in models instead put that in your controller.

I think there is no WHERE condition in your UPDATE Query this can be one of the problem of your issue..

be sure only that part of the code is being executed for your problem..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM