title: Auto increment ID not being correclty assigned in SQL database by PHP
body: <p>I am confused. Something isn't working in my new CMS I am designing. I am following this tutorial but am  changing some things (including using SQLite not MySQL) and adding more functionality. <a href="http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/" rel="nofollow">http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/</a></p>
  
  <p>Anyway, nearly everything works now but it just will not save new posts. The edit post functionality does work though.</p>
  
  <p>Here is the base form (the same form that works fine to edit a post):</p>
  
  <pre><code>&lt;h3&gt;Create a blog post&lt;/h3&gt;
  &lt;form method="POST" action="index.php?action=&lt;?php echo $posts['formAction']; ?&gt;"&gt;
       &lt;input type="hidden" id="id" name="id" value="&lt;?php echo $posts['post'] -&gt; id; ?&gt;" /&gt;
       &lt;p&gt;Title: &lt;input id="title" name="title" type="text" placeholder="Enter the post title" required autofocus maxlength="255" value="&lt;?php echo $posts['post'] -&gt; title; ?&gt;"/&gt;&lt;/p&gt;
       &lt;p&gt;Category: &lt;input id="category" name="category" type="text" placeholder="Enter the post category" required maxlength="255" value="&lt;?php echo $posts['post'] -&gt; category; ?&gt;"/&gt;&lt;/p&gt;
       &lt;p&gt;Tags: &lt;input id="tags" name="tags" type="text" placeholder="Enter some tags for the post" required value="&lt;?php echo $posts['post'] -&gt; tags; ?&gt;"/&gt;&lt;/p&gt;
       &lt;p&gt;Summary:&lt;/p&gt;&lt;textarea id="summary" name="summary" cols="100" rows="5" placeholder="Enter a short summary of the post's content" required&gt;&lt;?php echo $posts['post'] -&gt; summary; ?&gt;&lt;/textarea&gt;
       &lt;p&gt;Content:&lt;/p&gt;&lt;textarea id="body" name="body" cols="100" rows="30" placeholder="Enter the main content of the post" required&gt;&lt;?php echo $posts['post'] -&gt; body; ?&gt;&lt;/textarea&gt;
       &lt;p&gt;Publication date: &lt;/p&gt;&lt;input type="date" id="pubDate" name="pubDate" placeholder="DD-MM-YYYY" required maxlength="10" value="&lt;?php echo $posts['post'] -&gt; pubDate ? date('Y-m-d', $posts['post'] -&gt; pubDate) : ''; ?&gt;" /&gt;
       &lt;br /&gt;
       &lt;input id="saveChanges" name="saveChanges" type="submit" value="Create Post"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This ends up posting to the following function in index.php:</p>
  
  <pre><code>function newPost()
  {
      $posts = array();
      $posts['formAction'] = "newPost";
  
      if (isset($_POST['saveChanges']))
      {
          $post = new Post;
          $post -&gt; storePostValues($_POST);
          $post -&gt; insertPost();
          header("Location: index.php?status=changesSaved");
      }
      elseif (isset($_POST['cancel']))
      {
          header('Location: index.php');
      }
      else
      {
          $posts['post'] = new Post;
          require('editPost.php');
      }
  }
  </code></pre>
  
  <p>And finally the relevant code in the Post() class:</p>
  
  <pre><code>public function __construct($data = array()) {
      if (isset($data['id'])) $this -&gt; id = (int) $data['id'];
      if (isset($data['title'])) $this -&gt; title = $data['title'];
      if (isset($data['pubDate'])) $this -&gt; pubDate = (int) $data['pubDate'];
      if (isset($data['reviseDate'])) $this -&gt; reviseDate = (int) $data['reviseDate'];
      if (isset($data['category'])) $this -&gt; category = $data['category'];
      if (isset($data['tags'])) $this -&gt; tags = $data['tags'];
      if (isset($data['summary'])) $this -&gt; summary = $data['summary'];
      if (isset($data['body'])) $this -&gt; body = $data['body'];
      if (isset($data['views'])) $this -&gt; views = (int) $data['views'];
      if (isset($data['likes'])) $this -&gt; likes = (int) $data['likes'];
      if (isset($data['dislikes'])) $this -&gt; dislikes = (int) $data['dislikes'];
  }
  
          //Sets object properties from form POST values
  public function storePostValues($params) {
      $this -&gt; __construct($params);
  
      if (isset($params['pubDate'])) {
          $pubDate = explode('-', $params['pubDate']);
          if (count($pubDate) == 3) {
              list ($d, $m, $y) = $pubDate;
              $this -&gt; pubDate = mktime(0, 0, 0, $m, $d, $y);
          }
      }
      if (isset($params['reviseDate'])) {
          $reviseDate = explode('-', $params['reviseDate']);
          if (count($reviseDate) == 3) {
              list ($d, $m, $y) = $reviseDate;
              $this -&gt; reviseDate = mktime(0, 0, 0, $m, $d, $y);
          }
      }
  }
  
  public function insertPost() {
      if (!is_null($this -&gt; id)) trigger_error("Post::insertPost(): Post ID already assigned.", E_USER_ERROR);
      $dblocat = "sqlite:" . $_SERVER['DOCUMENT_ROOT'] . "/cgi-bin/blog.db";
      $dbcon = new PDO($dblocat);
      $dbcon-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
      $sqlstr = "INSERT INTO posts(title, pubDate, reviseDate, category, tags, summary, body, views, likes, dislikes) VALUES(:title, FROM_UNIXTIME(:pubDate), FROM_UNIXTIME(:reviseDate), :category, :tags, :summary, :body, :views, :likes, :dislikes)";
      $preparedstr = $dbcon -&gt; prepare($sqlstr);
      $preparedstr -&gt; bindValue(":title", $this -&gt; title, PDO::PARAM_STR);
      $preparedstr -&gt; bindValue(":pubDate", $this -&gt; pubDate, PDO::PARAM_INT);
      $preparedstr -&gt; bindValue(":reviseDate", $this -&gt; reviseDate, PDO::PARAM_INT);
      $preparedstr -&gt; bindValue(":category", $this -&gt; category, PDO::PARAM_STR);
      $preparedstr -&gt; bindValue(":tags", $this -&gt; tags, PDO::PARAM_STR);
      $preparedstr -&gt; bindValue(":summary", $this -&gt; summary, PDO::PARAM_STR);
      $preparedstr -&gt; bindValue(":body", $this -&gt; body, PDO::PARAM_STR);
      $preparedstr -&gt; bindValue(":views", 0, PDO::PARAM_INT);
      $preparedstr -&gt; bindValue(":likes", 0, PDO::PARAM_INT);
      $preparedstr -&gt; bindValue(":dislikes", 0, PDO::PARAM_INT);
      $preparedstr -&gt; execute();
      $this -&gt; id = $dbcon -&gt; lastInsertId();
      $dbcon = null;
  }
  </code></pre>
  
  <p>So what happens? Well, every time I save a post that error in the insertPost() function is triggered saying that the post ID already exists... but it is set to auto increment in the database so I'm not quite sure what is going on... I'm relatively new to PHP and have spent hours now trying to see why this isn't working.</p>
  
  <p>Any advice would be appreciated and I can send more code if required.
  Again, I am basing it on this tutorial but I have modified it and now use SQLite.
  <a href="http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/" rel="nofollow">http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/</a></p>
  
  <p>Thanks in advance,
  Ilmiont</p>