title: Codeigniter 3 blogging application bug: post slug updates even if there are no changes to the post's title
body: <p>I am working on a basic blog application with <strong>Codeigniter 3.1.8</strong> and <strong>Bootstrap 4</strong>.</p>
  
  <p>For this application, the posts are <em>uniquely identified by slug</em> and displayed to the viewers. </p>
  
  <p>I have created a mechanism that <em>prevents duplicate slugs</em> in case there are duplicate post titles, by adding numbers to the slugs. This mechanism is included in both the <code>create()</code> and <code>update()</code> methods of the Posts controller.</p>
  
  <p>Inside the <code>update()</code> method this mechanism functions imperfectly: the post slug updates even if there are no changes to the post's title (<em>my-post-title</em> becomes <em>my-post-title-1</em> if the "Update" button is pressed even id there is no duplicate of the post's title). </p>
  
  <p>Here is the <code>create()</code> method in the <strong>Posts</strong> controller. There are no problems with it:</p>
  
  <pre><code> public function create() {
  
      // Only logged in users can create posts
      if (!$this-&gt;session-&gt;userdata('is_logged_in')) {
          redirect('login');
      }
  
      $data = $this-&gt;get_data();
      $data['tagline'] = "Add New Post";
  
      if ($data['categories']) {
          foreach ($data['categories'] as &amp;$category) {
              $category-&gt;posts_count = $this-&gt;Posts_model-&gt;count_posts_in_category($category-&gt;id);
          }
      }
  
      $this-&gt;form_validation-&gt;set_rules('title', 'Title', 'required');
      $this-&gt;form_validation-&gt;set_rules('desc', 'Short description', 'required');
      $this-&gt;form_validation-&gt;set_rules('body', 'Body', 'required');
      $this-&gt;form_validation-&gt;set_error_delimiters('&lt;p class="error-message"&gt;', '&lt;/p&gt;');
  
      if($this-&gt;form_validation-&gt;run() === FALSE){
          $this-&gt;load-&gt;view('partials/header', $data);
          $this-&gt;load-&gt;view('dashboard/create-post');
          $this-&gt;load-&gt;view('partials/footer');
      } else {
          // Create slug (from title)
          $slug = url_title($this-&gt;input-&gt;post('title'), 'dash', TRUE);
          $slugcount = $this-&gt;Posts_model-&gt;slug_count($slug);
          if ($slugcount &gt; 0) {
              $slug = $slug."-".$slugcount;
          }
  
          // Upload image
          $config['upload_path'] = './assets/img/posts';
          $config['allowed_types'] = 'jpg|png';
          $config['max_size'] = '2048';
  
          $this-&gt;load-&gt;library('upload', $config);
  
          if(!$this-&gt;upload-&gt;do_upload()){
              $errors = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
              $post_image = 'default.jpg';
          } else {
              $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
              $post_image = $_FILES['userfile']['name'];
          }
  
          $this-&gt;Posts_model-&gt;create_post($post_image, $slug);
          $this-&gt;session-&gt;set_flashdata('post_created', 'Your post has been created');
          redirect('/');
      }
  }
  </code></pre>
  
  <p>Here is the <code>update()</code> method in the <strong>Posts</strong> controller:</p>
  
  <pre><code>public function update() {
      // Form data validation rules
      $this-&gt;form_validation-&gt;set_rules('title', 'Title', 'required',  array('required' =&gt; 'The %s field can not be empty'));
      $this-&gt;form_validation-&gt;set_rules('desc', 'Short description', 'required',  array('required' =&gt; 'The %s field can not be empty'));
      $this-&gt;form_validation-&gt;set_rules('body', 'Body', 'required',  array('required' =&gt; 'The %s field can not be empty'));
      $this-&gt;form_validation-&gt;set_error_delimiters('&lt;p class="error-message"&gt;', '&lt;/p&gt;');
  
      $id = $this-&gt;input-&gt;post('id');
  
      // Update slug (from title)
      if ($this-&gt;form_validation-&gt;run()) {
          $slug = url_title($this-&gt;input-&gt;post('title'), 'dash', TRUE);
          $slugcount = $this-&gt;Posts_model-&gt;slug_count($slug);
          if ($slugcount &gt; 0) {
              $slug = $slug."-".$slugcount;
          }
      } else {
          $slug = $this-&gt;input-&gt;post('slug');
      }
  
      // Upload image
      $config['upload_path'] = './assets/img/posts';
      $config['allowed_types'] = 'jpg|png';
      $config['max_size'] = '2048';
  
      $this-&gt;load-&gt;library('upload', $config);
  
      if (isset($_FILES['userfile']['name']) &amp;&amp; $_FILES['userfile']['name'] != null) {
          // Use name field in do_upload method
          if (!$this-&gt;upload-&gt;do_upload('userfile')) {
              $errors = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
  
          } else {
              $data = $this-&gt;upload-&gt;data();
              $post_image = $data[ 'raw_name'].$data[ 'file_ext'];
          }
      }
      else {
          $post_image = $this-&gt;input-&gt;post('postimage');
      }
  
      if ($this-&gt;form_validation-&gt;run()) {
          $this-&gt;Posts_model-&gt;update_post($id, $post_image, $slug);
          $this-&gt;session-&gt;set_flashdata('post_updated', 'Your post has been updated');
          redirect('/' . $slug);
      } else {
          $this-&gt;form_validation-&gt;run();
          $this-&gt;session-&gt;set_flashdata('errors', validation_errors());
          redirect('/dashboard/posts/edit/' . $slug);
      }
  }
  </code></pre>
  
  <p>In the <strong>Posts_model</strong> model I have:</p>
  
  <pre><code>// Count the slugs in the posts table
  public function slug_count($slug){
      $this-&gt;db-&gt;select('count(*) as slugcount');
      $this-&gt;db-&gt;from('posts');
      $this-&gt;db-&gt;where('slug', $slug);
      $query = $this-&gt;db-&gt;get();
      return $query-&gt;row(0)-&gt;slugcount;
  }
  
  // Update post
  public function update_post($id, $post_image, $slug) {
      $data = [
          'title' =&gt; $this-&gt;input-&gt;post('title'),
          'slug' =&gt; $slug,
          'description' =&gt; $this-&gt;input-&gt;post('desc'),
          'content' =&gt; $this-&gt;input-&gt;post('body'),
          'post_image' =&gt; $post_image,
          'cat_id' =&gt; $this-&gt;input-&gt;post('category'),
          'updated_at' =&gt; date('Y-m-d H:i:s')
      ];
  
      $this-&gt;db-&gt;where('id', $id);
      return $this-&gt;db-&gt;update('posts', $data);
  }
  </code></pre>
  
  <p>How shall I change the code above so that the bug described above is fixed?  </p>