title: My Codeigniter method for safely deleting database entries
body: <p>I've been searching about deleting db entries in Codeigniter and I finally created a solution that I think is secure. I would really appreciate any feedback! I'm not sure if I'm doing this right..</p>
  
  <p><strong>Advantages:</strong></p>
  
  <ul>
  <li><p>Uses POST request</p></li>
  <li><p>ID of entry to be deleted is<br>
  validated</p></li>
  <li><p>Uses CSRF protection (automatically<br>
  generated by Codeigniter)</p></li>
  </ul>
  
  <p>In my example I'm deleting user submitted links (a DB table row contains a link title, link URL, an link description). </p>
  
  <p>HTML: Database entires are contained within a form. Each entry has a form button with the respective link id in the <code>id</code> attribute. </p>
  
  <pre><code>&lt;?php echo form_open('profile/remove_link'); ?&gt;
  
  &lt;?php echo form_hidden('link_id', ''); //value will be populated via jquery ?&gt;
  
  &lt;ul id="user_links"&gt;
      &lt;?php foreach($query as $row): ?&gt;
  
      &lt;li&gt;&lt;?php echo $row-&gt;link_title; ?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo auto_link($row-&gt;link_url, 'url', TRUE); ?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo $row-&gt;link_description; ?&gt;&lt;/li&gt;
  
      &lt;button type="submit" class="remove" id="&lt;?php echo $row-&gt;link_id ?&gt;"  value="remove"&gt;Remove Link&lt;/button&gt;
  
      &lt;?php endforeach; ?&gt;
  &lt;/ul&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>JQUERY: When user clicks on the <code>remove</code> button, the respective link id is added to the the hidden text input named <code>link_id</code>. </p>
  
  <pre><code>$(document).ready(function(){
      $('.remove').click(function() {
          var link_to_remove = $(this).attr("id");
          $("input[name=link_id]").val(link_to_remove);
      }); 
  }); 
  </code></pre>
  
  <p>Upon clicking a remove button, it sends the id of link to be removed to controller <code>profile</code> and function <code>remove_link</code></p>
  
  <pre><code>    function remove_link()
      {
          $this-&gt;load-&gt;model('Profile_model');
          $links_data['query'] = $this-&gt;Profile_model-&gt;links_read(); //get links from db to add in view
  
          //Validation
          $this-&gt;form_validation-&gt;set_rules('link_id', 'Link ID', 'trim|required|xss_clean|max_length[11]|numeric'); //validate link id
  
          if ($this-&gt;form_validation-&gt;run() == FALSE) //if validation rules fail
          {           
              $this-&gt;load-&gt;view('profile/edit_links_view', $links_data);
          }
          else //success
          {
              $link_id =  $this-&gt;input-&gt;post('link_id'); //get id of link to be deleted
              $seg = 'user_links'; //used to redirect back to user links page
              $this-&gt;Profile_model-&gt;links_delete($link_id, $seg); //send link id to model function            
          }       
      }
  </code></pre>
  
  <p>MODEL </p>
  
  <pre><code>    function links_delete($link_id, $seg)
      {
          $this-&gt;db-&gt;where('user_id', $this-&gt;tank_auth-&gt;get_user_id());
          $this-&gt;db-&gt;where('link_id', $link_id);
          $this-&gt;db-&gt;delete('user_links'); 
          redirect("/profile/$seg/");         
      }
  </code></pre>