Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy is my update query creating new row?
    text
    copied!<p>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. </p> <p>Here is the form:</p> <pre><code>&lt;form action="&lt;?=base_url();?&gt;blog/new_post" method="post" name="write_new"&gt; &lt;input type="hidden" name="user" value="&lt;?=$user?&gt;"&gt; &lt;label&gt;Title:&lt;/label&gt;&lt;br&gt; &lt;input type="text" name="title" value="&lt;?php if(isset($query-&gt;title)) { echo $query-&gt;title; }?&gt;" size="50"&gt;&lt;br&gt; &lt;label&gt;Date:&lt;/label&gt;&lt;br&gt; &lt;input type="text" name="date" size="46" value="&lt;?php if(isset($query-&gt;date)) { echo $query-&gt;date; }?&gt;" /&gt; &lt;script language="JavaScript"&gt; new tcal ({ // javascript calender thingy // form name 'formname': 'write_new', // input name 'controlname': 'date' }); &lt;/script&gt; &lt;br&gt;&lt;br&gt; &lt;label&gt;Article:&lt;/label&gt;&lt;br&gt; &lt;textarea name="content" style="width:100%;height:300px;"&gt;&lt;?php if(isset($query-&gt;article)) { echo $query-&gt;article; }?&gt;&lt;/textarea&gt;&lt;br&gt; &lt;select name="category"&gt; &lt;option value="news"&gt;Dreamsy News&lt;/option&gt; &lt;option value="web"&gt;From the Interwebs&lt;/option&gt; &lt;/select&gt; &lt;input type="submit"/&gt; &lt;/form&gt; &lt;div class="clearboth"&gt;&lt;/div&gt; </code></pre> <p>Here's the model:</p> <pre><code>function edit_post($title, $date, $author, $article, $category, $id) { $this-&gt;load-&gt;helper('form'); //loads the CI form helper $this-&gt;load-&gt;library('form_validation'); //loads the form validation class $this-&gt;form_validation-&gt;set_rules('title', 'title', 'required|min_length[3]|max_length[40]|xss_clean'); $this-&gt;form_validation-&gt;set_rules('date', 'date', 'required|min_length[5]|max_length[15]|xss_clean'); $this-&gt;form_validation-&gt;set_rules('category', 'category', 'required|xss_clean'); $this-&gt;form_validation-&gt;set_rules('content', 'article', 'required|xss_clean'); $this-&gt;form_validation-&gt;set_rules('user', 'you are not logged in', 'required'); if ($this-&gt;form_validation-&gt;run() == FALSE) //If the form does not validate { $this-&gt;load-&gt;vars(array('title' =&gt; 'Error')); $this-&gt;template-&gt;write_view('content', 'error/new_post_fail'); $this-&gt;template-&gt;render(); } else { $article = htmlentities($article, ENT_QUOTES); /*$data = array( 'title' =&gt; $title, 'date' =&gt; $date, 'author' =&gt; $author, 'article' =&gt; $article, 'category' =&gt; $category );*/ //$this-&gt;db-&gt;where('id', $id); //$this-&gt;db-&gt;update('blog', $data); $query = 'UPDATE blog SET title = '.$title.' date = '.$date.', author = '.$author.', article = '.$article.', category = '.$category; $this-&gt;db-&gt;query($query); redirect(base_url().'blog'); } </code></pre> <p>And here's the controller:</p> <pre><code>public function write() { $id = $this-&gt;uri-&gt;segment(3); $query = 'SELECT * FROM blog WHERE id = '.$id; $query = $this-&gt;db-&gt;query($query); $query = $query-&gt;row(); $title = $query-&gt;title; $user = $this-&gt;session-&gt;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-&gt;load-&gt;helper('form'); //loads the CI form helper $this-&gt;load-&gt;library('form_validation'); //loads the form validation class $this-&gt;load-&gt;vars($vars); $this-&gt;template-&gt;write_view('content', 'blog/write'); $this-&gt;template-&gt;render(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload